ui
ui copied to clipboard
When update data on Datatable it navigate me the first page
my code where I placed my Datatable
<DataTable filtred="code" columns={columnsRender} data={state?.data || []} />
DataTable component
interface DataTableProps<TData, TValue> {
// eslint-disable-next-line @typescript-eslint/array-type
columns: ColumnDef<TData, TValue>[]
data: TData[]
filtred?: string
}
export function DataTable<TData, TValue>({
columns,
data,
filtred
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = React.useState<SortingState>([])
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
[]
)
const [columnVisibility, setColumnVisibility] =
React.useState<VisibilityState>({})
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
onColumnVisibilityChange: setColumnVisibility,
state: {
sorting,
columnFilters,
columnVisibility,
},
})
return (
<div>
<div className="flex items-center py-4">
{filtred &&
<Input
placeholder={`Filtrer par ${filtred}`}
value={(table.getColumn(filtred)?.getFilterValue() as string) ?? ""}
onChange={(event) =>
table.getColumn(filtred)?.setFilterValue(event.target.value)
}
className="max-w-sm bg-gray-100"
/>}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" className="ml-auto bg-primary text-white">
Columns
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{table
.getAllColumns()
.filter(
(column) => column.getCanHide()
)
.map((column) => {
return (
<DropdownMenuCheckboxItem
key={column.id}
className="capitalize"
checked={column.getIsVisible()}
onCheckedChange={(value) => { column.toggleVisibility(!!value) }
}
>
{column.id}
</DropdownMenuCheckboxItem>
)
})}
</DropdownMenuContent>
</DropdownMenu>
</div>
<div className="rounded-md border">
<Table>
<TableHeader className="text-white font-extrabold">
{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>
)}
</TableBody>
</Table>
</div>
<div className="flex items-center justify-end space-x-2 py-4">
<Button
variant="outline"
size="sm"
onClick={() => { table.previousPage() }}
disabled={!table.getCanPreviousPage()}
>
Previous
</Button>
<Button
variant="outline"
size="sm"
onClick={() => { table.nextPage() }}
disabled={!table.getCanNextPage()}
>
Next
</Button>
</div>
</div>
)
}
Did You find any solution , I have the same problem , i copied the Example code and changed only data
I faced the same error and my problem is the data is generated in a loop. I fixed it by wrapping it in useMemo:
Met the same issue and found this table option autoResetPageIndex in tanstack table. Setting it to false will keep the table in the current page when the data is updated. https://tanstack.com/table/v8/docs/api/features/pagination#autoresetpageindex
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.