database view dinamico y personalizado completo
This commit is contained in:
334
nuxt4-app/app/components/clientes/VistaTablaClientes.vue
Normal file
334
nuxt4-app/app/components/clientes/VistaTablaClientes.vue
Normal file
@@ -0,0 +1,334 @@
|
||||
<template>
|
||||
<UCard class="brand-card border border-transparent">
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<h2 class="text-xl font-bold brand-section-title">Vista Tabla de Clientes</h2>
|
||||
<p class="text-sm text-[var(--brand-text-muted)] mt-1">
|
||||
{{ props.records.length }} clientes registrados
|
||||
</p>
|
||||
</div>
|
||||
<UButton
|
||||
:label="dateFormat === 'short' ? 'Fecha Larga' : 'Fecha Corta'"
|
||||
color="neutral"
|
||||
variant="outline"
|
||||
icon="i-lucide-calendar"
|
||||
@click="toggleDateFormat"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Table Controls -->
|
||||
<div class="flex items-center gap-2 px-4 py-3.5 overflow-x-auto border-b border-[var(--brand-border)]">
|
||||
<UInput
|
||||
v-model="globalFilter"
|
||||
class="max-w-sm min-w-[12ch]"
|
||||
placeholder="Buscar en todos los campos..."
|
||||
icon="i-lucide-search"
|
||||
/>
|
||||
|
||||
<UDropdownMenu
|
||||
v-if="table?.tableApi"
|
||||
:items="columnVisibilityItems"
|
||||
:content="{ align: 'end' }"
|
||||
>
|
||||
<UButton
|
||||
label="Columnas"
|
||||
color="neutral"
|
||||
variant="outline"
|
||||
trailing-icon="i-lucide-chevron-down"
|
||||
class="ml-auto"
|
||||
aria-label="Selector de columnas visibles"
|
||||
/>
|
||||
</UDropdownMenu>
|
||||
</div>
|
||||
|
||||
<!-- Table Component -->
|
||||
<UTable
|
||||
ref="table"
|
||||
:data="limitedRecords"
|
||||
:columns="tableColumns"
|
||||
:global-filter="globalFilter"
|
||||
sticky
|
||||
class="h-96"
|
||||
/>
|
||||
|
||||
<!-- Table Footer -->
|
||||
<template #footer>
|
||||
<div class="flex flex-col gap-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-[var(--brand-text-muted)]">
|
||||
Mostrando {{ startRecord }} - {{ endRecord }} de {{ totalRecords }} registros
|
||||
</span>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<UButton
|
||||
icon="i-lucide-chevron-left"
|
||||
color="neutral"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
:disabled="currentPage === 1"
|
||||
@click="previousPage"
|
||||
/>
|
||||
<span class="text-sm text-[var(--brand-text-muted)] px-2">
|
||||
Página {{ currentPage }} de {{ totalPages }}
|
||||
</span>
|
||||
<UButton
|
||||
icon="i-lucide-chevron-right"
|
||||
color="neutral"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
:disabled="currentPage === totalPages"
|
||||
@click="nextPage"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</UCard>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { h, ref, computed, resolveComponent } from 'vue'
|
||||
import { upperFirst } from 'scule'
|
||||
import type { TableColumn } from '@nuxt/ui'
|
||||
|
||||
interface ClienteRecord extends Record<string, unknown> {
|
||||
id: number
|
||||
created_at: string
|
||||
updated_at: string
|
||||
name: string
|
||||
cedula?: number
|
||||
ubicacion?: string
|
||||
grupo_estudio?: string
|
||||
empleado?: boolean
|
||||
avatar_url?: string
|
||||
telefono?: string
|
||||
idciat?: number
|
||||
}
|
||||
|
||||
interface Props {
|
||||
records: ClienteRecord[]
|
||||
}
|
||||
|
||||
const props = defineProps<Props>()
|
||||
|
||||
const UButton = resolveComponent('UButton')
|
||||
const UBadge = resolveComponent('UBadge')
|
||||
const UIcon = resolveComponent('UIcon')
|
||||
const UAvatar = resolveComponent('UAvatar')
|
||||
|
||||
const globalFilter = ref('')
|
||||
const table = useTemplateRef<{ tableApi?: any }>('table')
|
||||
const currentPage = ref(1)
|
||||
const recordsPerPage = 100
|
||||
const dateFormat = ref<'short' | 'long'>('short')
|
||||
|
||||
function toggleDateFormat() {
|
||||
dateFormat.value = dateFormat.value === 'short' ? 'long' : 'short'
|
||||
}
|
||||
|
||||
// Paginación
|
||||
const totalRecords = computed(() => props.records.length)
|
||||
const totalPages = computed(() => Math.ceil(totalRecords.value / recordsPerPage))
|
||||
|
||||
const startRecord = computed(() => {
|
||||
if (totalRecords.value === 0) return 0
|
||||
return (currentPage.value - 1) * recordsPerPage + 1
|
||||
})
|
||||
|
||||
const endRecord = computed(() => {
|
||||
const end = currentPage.value * recordsPerPage
|
||||
return Math.min(end, totalRecords.value)
|
||||
})
|
||||
|
||||
const limitedRecords = computed(() => {
|
||||
const start = (currentPage.value - 1) * recordsPerPage
|
||||
const end = start + recordsPerPage
|
||||
return (props.records || []).slice(start, end)
|
||||
})
|
||||
|
||||
function nextPage() {
|
||||
if (currentPage.value < totalPages.value) {
|
||||
currentPage.value++
|
||||
}
|
||||
}
|
||||
|
||||
function previousPage() {
|
||||
if (currentPage.value > 1) {
|
||||
currentPage.value--
|
||||
}
|
||||
}
|
||||
|
||||
// Columnas seleccionadas
|
||||
const selectedColumns = [
|
||||
'id',
|
||||
'name',
|
||||
'cedula',
|
||||
'telefono',
|
||||
'ubicacion',
|
||||
'grupo_estudio',
|
||||
'empleado',
|
||||
'idciat',
|
||||
'created_at',
|
||||
'updated_at'
|
||||
]
|
||||
|
||||
const tableColumns = computed((): TableColumn<Record<string, unknown>>[] => {
|
||||
if (!limitedRecords.value.length) return []
|
||||
|
||||
const firstRow = limitedRecords.value[0]
|
||||
if (!firstRow) return []
|
||||
|
||||
const availableColumns = selectedColumns.filter(col => col in firstRow)
|
||||
|
||||
return availableColumns.map((column: string) => ({
|
||||
accessorKey: column,
|
||||
header: ({ column: tableColumn }) => {
|
||||
const isSorted = tableColumn.getIsSorted()
|
||||
|
||||
return h(UButton, {
|
||||
color: 'neutral',
|
||||
variant: 'ghost',
|
||||
label: upperFirst(column),
|
||||
icon: isSorted ? (isSorted === 'asc' ? 'i-lucide-arrow-up-narrow-wide' : 'i-lucide-arrow-down-wide-narrow') : 'i-lucide-arrow-up-down',
|
||||
class: '-mx-2.5',
|
||||
onClick: () => tableColumn.toggleSorting(tableColumn.getIsSorted() === 'asc')
|
||||
})
|
||||
},
|
||||
cell: ({ row }) => formatCellValue(row.getValue(column), column, row.original as ClienteRecord)
|
||||
}))
|
||||
})
|
||||
|
||||
const columnVisibilityItems = computed((): any[] => {
|
||||
if (!table.value?.tableApi) return []
|
||||
|
||||
return table.value.tableApi
|
||||
.getAllColumns()
|
||||
.filter((column: any) => column.getCanHide())
|
||||
.map((column: any) => ({
|
||||
label: upperFirst(column.id),
|
||||
type: 'checkbox' as const,
|
||||
checked: column.getIsVisible(),
|
||||
onUpdateChecked(checked: boolean) {
|
||||
table.value?.tableApi?.getColumn(column.id)?.toggleVisibility(!!checked)
|
||||
},
|
||||
onSelect(e?: Event) {
|
||||
e?.preventDefault()
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
function formatCellValue(value: unknown, column: string, row: ClienteRecord): any {
|
||||
if (value === null || value === undefined) {
|
||||
return '—'
|
||||
}
|
||||
|
||||
// ID column - badge con formato C-####
|
||||
if (column === 'id' && typeof value === 'number') {
|
||||
return h(UBadge, {
|
||||
label: `C-${value}`,
|
||||
color: 'primary',
|
||||
variant: 'subtle',
|
||||
size: 'md',
|
||||
class: 'rounded-full'
|
||||
})
|
||||
}
|
||||
|
||||
// idciat - badge con formato CIAT-####
|
||||
if (column === 'idciat' && typeof value === 'number') {
|
||||
return h(UBadge, {
|
||||
label: `CIAT-${value}`,
|
||||
color: 'info',
|
||||
variant: 'subtle',
|
||||
size: 'md',
|
||||
class: 'rounded-full'
|
||||
})
|
||||
}
|
||||
|
||||
// name - con avatar si está disponible
|
||||
if (column === 'name' && typeof value === 'string') {
|
||||
return h('div', { class: 'flex items-center gap-2' }, [
|
||||
row.avatar_url ? h(UAvatar, {
|
||||
src: row.avatar_url,
|
||||
alt: value,
|
||||
size: 'xs'
|
||||
}) : h(UIcon, {
|
||||
name: 'i-lucide-user-circle',
|
||||
class: 'w-6 h-6 text-gray-400'
|
||||
}),
|
||||
h('span', { class: 'font-medium' }, value)
|
||||
])
|
||||
}
|
||||
|
||||
// empleado - icono booleano
|
||||
if (column === 'empleado' && typeof value === 'boolean') {
|
||||
return h(UIcon, {
|
||||
name: value ? 'i-lucide-briefcase' : 'i-lucide-user',
|
||||
class: value ? 'text-purple-600 w-5 h-5' : 'text-gray-400 w-5 h-5'
|
||||
})
|
||||
}
|
||||
|
||||
// cedula - formato especial
|
||||
if (column === 'cedula' && typeof value === 'number') {
|
||||
return h(UBadge, {
|
||||
label: String(value),
|
||||
color: 'neutral',
|
||||
variant: 'soft',
|
||||
size: 'sm',
|
||||
class: 'font-mono'
|
||||
})
|
||||
}
|
||||
|
||||
// telefono - con icono
|
||||
if (column === 'telefono' && typeof value === 'string') {
|
||||
return h('div', { class: 'flex items-center gap-1.5' }, [
|
||||
h(UIcon, {
|
||||
name: 'i-lucide-phone',
|
||||
class: 'w-4 h-4 text-green-600'
|
||||
}),
|
||||
h('span', { class: 'font-mono text-sm' }, value)
|
||||
])
|
||||
}
|
||||
|
||||
// ubicacion - con icono
|
||||
if (column === 'ubicacion' && typeof value === 'string') {
|
||||
return h('div', { class: 'flex items-center gap-1.5' }, [
|
||||
h(UIcon, {
|
||||
name: 'i-lucide-map-pin',
|
||||
class: 'w-4 h-4 text-red-500'
|
||||
}),
|
||||
h('span', { class: 'text-sm' }, value)
|
||||
])
|
||||
}
|
||||
|
||||
// grupo_estudio - badge
|
||||
if (column === 'grupo_estudio' && typeof value === 'string') {
|
||||
return h(UBadge, {
|
||||
label: value,
|
||||
color: 'secondary',
|
||||
variant: 'subtle',
|
||||
size: 'sm'
|
||||
})
|
||||
}
|
||||
|
||||
// Formatear fechas
|
||||
if ((column === 'created_at' || column === 'updated_at') && typeof value === 'string' && value.includes('T')) {
|
||||
try {
|
||||
const date = new Date(value)
|
||||
if (dateFormat.value === 'long') {
|
||||
return date.toLocaleDateString('es-HN', {
|
||||
day: 'numeric',
|
||||
month: 'long',
|
||||
year: 'numeric'
|
||||
})
|
||||
}
|
||||
return date.toLocaleDateString('es-HN')
|
||||
} catch {
|
||||
return String(value)
|
||||
}
|
||||
}
|
||||
|
||||
return String(value).substring(0, 100)
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user