diff --git a/app/ui/Table.vue b/app/ui/Table.vue index 60b116e..f616e25 100644 --- a/app/ui/Table.vue +++ b/app/ui/Table.vue @@ -15,23 +15,47 @@ type TableProps = { data: any pagination?: boolean pageSize?: number + search?: boolean } const props = withDefaults(defineProps(), { pagination: false, pageSize: 10, + search: false, }) const currentPage = ref(1) +const query = ref("") const pageSize = computed(() => Math.max(1, Math.floor(props.pageSize))) +const filteredData = computed(() => { + const normalizedQuery = query.value.trim().toLocaleLowerCase() + const indexedData = props.data.map((item: any, index: number) => ({ + item, + index, + })) + + if (!props.search || !normalizedQuery) return indexedData + + return indexedData.filter(({ item }: { item: any }) => + props.rows.some((row) => + String(item[row.name] ?? "") + .toLocaleLowerCase() + .includes(normalizedQuery), + ), + ) +}) const pageCount = computed(() => - Math.max(1, Math.ceil(props.data.length / pageSize.value)), + Math.max(1, Math.ceil(filteredData.value.length / pageSize.value)), ) -const visibleData = computed(() => { - if (!props.pagination) return props.data +const pagedData = computed(() => { + if (!props.pagination) return filteredData.value const start = (currentPage.value - 1) * pageSize.value - return props.data.slice(start, start + pageSize.value) + return filteredData.value.slice(start, start + pageSize.value) +}) + +watch(query, () => { + currentPage.value = 1 }) watch(pageCount, (count) => { @@ -40,6 +64,13 @@ watch(pageCount, (count) => {