Skip to content

Commit

Permalink
fix: added filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
ThianHooi committed Jul 28, 2024
1 parent 40715eb commit 6730dbf
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
121 changes: 78 additions & 43 deletions app/modules/dashboard/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
flexRender,
getCoreRowModel,
useReactTable,
getFilteredRowModel,
ColumnFiltersState,
} from '@tanstack/react-table';

import {
Expand All @@ -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<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
Expand All @@ -26,58 +30,89 @@ export function DataTable<TData, TValue>({
data,
onRowClick,
}: DataTableProps<TData, TValue>) {
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);

const parsedColumns = getColumns(onRowClick) as ColumnDef<TData, TValue>[];

const table = useReactTable({
data,
columns: parsedColumns,
getCoreRowModel: getCoreRowModel(),
onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
state: { columnFilters },
});

return (
<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && 'selected'}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
<div>
<div className="flex items-center py-4">
<DebounceInput
placeholder="Filter by name or location..."
className="max-w-sm"
value={
(table.getColumn('property_name')?.getFilterValue() as string) || ''
}
onChange={(value) =>
setColumnFilters([
{
id: 'property_name',
value: value,
},
])
}
/>
</div>

<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && 'selected'}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No results.
</TableCell>
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
)}
</TableBody>
</Table>
</div>
</div>
);
}
7 changes: 7 additions & 0 deletions app/modules/dashboard/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export const columns: ColumnDef<Video>[] = [
{
accessorKey: 'property_name',
header: 'Property Name',
filterFn: (row, id, value) => {
const name = row.getValue<string>('property_name')?.toLowerCase();
const location = row.getValue<string>('location')?.toLowerCase();
const search = value.toLowerCase();

return name?.includes(search) || location?.includes(search);
},
},
{
accessorKey: 'location',
Expand Down

0 comments on commit 6730dbf

Please sign in to comment.