From 6730dbfb2bb2e4f3f759d7cb290225124fe89b2d Mon Sep 17 00:00:00 2001 From: ooithianhooi Date: Sun, 28 Jul 2024 17:01:37 +0800 Subject: [PATCH] fix: added filtering --- app/modules/dashboard/DataTable.tsx | 121 ++++++++++++++++++---------- app/modules/dashboard/columns.tsx | 7 ++ 2 files changed, 85 insertions(+), 43 deletions(-) diff --git a/app/modules/dashboard/DataTable.tsx b/app/modules/dashboard/DataTable.tsx index e881302..c8890f2 100644 --- a/app/modules/dashboard/DataTable.tsx +++ b/app/modules/dashboard/DataTable.tsx @@ -3,6 +3,8 @@ import { flexRender, getCoreRowModel, useReactTable, + getFilteredRowModel, + ColumnFiltersState, } from '@tanstack/react-table'; import { @@ -14,6 +16,8 @@ import { TableRow, } from '~/components/ui/table'; import { getColumns } from './columns'; +import { useState } from 'react'; +import { DebounceInput } from '~/components/ui/debounce-input'; interface DataTableProps { columns: ColumnDef[]; @@ -26,58 +30,89 @@ export function DataTable({ data, onRowClick, }: DataTableProps) { + const [columnFilters, setColumnFilters] = useState([]); + const parsedColumns = getColumns(onRowClick) as ColumnDef[]; - + const table = useReactTable({ data, columns: parsedColumns, getCoreRowModel: getCoreRowModel(), + onColumnFiltersChange: setColumnFilters, + getFilteredRowModel: getFilteredRowModel(), + state: { columnFilters }, }); return ( -
- - - {table.getHeaderGroups().map((headerGroup) => ( - - {headerGroup.headers.map((header) => { - return ( - - {header.isPlaceholder - ? null - : flexRender( - header.column.columnDef.header, - header.getContext() - )} - - ); - })} - - ))} - - - {table.getRowModel().rows?.length ? ( - table.getRowModel().rows.map((row) => ( - - {row.getVisibleCells().map((cell) => ( - - {flexRender(cell.column.columnDef.cell, cell.getContext())} - - ))} +
+
+ + setColumnFilters([ + { + id: 'property_name', + value: value, + }, + ]) + } + /> +
+ +
+
+ + {table.getHeaderGroups().map((headerGroup) => ( + + {headerGroup.headers.map((header) => { + return ( + + {header.isPlaceholder + ? null + : flexRender( + header.column.columnDef.header, + header.getContext() + )} + + ); + })} + + ))} + + + {table.getRowModel().rows?.length ? ( + table.getRowModel().rows.map((row) => ( + + {row.getVisibleCells().map((cell) => ( + + {flexRender( + cell.column.columnDef.cell, + cell.getContext() + )} + + ))} + + )) + ) : ( + + + No results. + - )) - ) : ( - - - No results. - - - )} - -
+ )} + + +
); } diff --git a/app/modules/dashboard/columns.tsx b/app/modules/dashboard/columns.tsx index adfbf8f..aed61d5 100644 --- a/app/modules/dashboard/columns.tsx +++ b/app/modules/dashboard/columns.tsx @@ -44,6 +44,13 @@ export const columns: ColumnDef