Skip to content

Commit 844c805

Browse files
committed
feat(server side pagination on table): Implement DataTablePagination component for data table with row selection, page count, and navigation controls.
1 parent 31bdf17 commit 844c805

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Table } from '@tanstack/react-table';
2+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './select';
3+
import { Button } from './button';
4+
import { ChevronLeftIcon, ChevronRightIcon, ChevronsLeft, ChevronsRight } from 'lucide-react';
5+
6+
interface DataTablePaginationProps<TData> {
7+
table: Table<TData>;
8+
}
9+
10+
export function DataTablePagination<TData>({ table }: DataTablePaginationProps<TData>) {
11+
return (
12+
<div className="flex items-center justify-between px-2 space-x-2 py-4">
13+
<div className="flex-1 text-sm text-muted-foreground">
14+
{table.getFilteredSelectedRowModel().rows.length} of {table.getFilteredRowModel().rows.length} row(s) selected.
15+
</div>
16+
<div className="flex items-center space-x-6 lg:space-x-8">
17+
<div className="flex items-center space-x-2">
18+
<p className="text-sm font-medium">Rows per page</p>
19+
<Select
20+
value={`${table.getState().pagination.pageSize}`}
21+
onValueChange={(value) => {
22+
table.setPageSize(Number(value));
23+
}}
24+
>
25+
<SelectTrigger className="h-8 w-[70px]">
26+
<SelectValue placeholder={table.getState().pagination.pageSize} />
27+
</SelectTrigger>
28+
<SelectContent side="top">
29+
{[10, 20, 30, 40, 50].map((pageSize) => (
30+
<SelectItem key={pageSize} value={`${pageSize}`}>
31+
{pageSize}
32+
</SelectItem>
33+
))}
34+
</SelectContent>
35+
</Select>
36+
</div>
37+
<div className="flex w-[100px] items-center justify-center text-sm font-medium">
38+
Page {table.getState().pagination.pageIndex + 1} of {table.getPageCount()}
39+
</div>
40+
<div className="flex items-center space-x-2">
41+
<Button
42+
variant="outline"
43+
className="hidden h-8 w-8 p-0 lg:flex"
44+
onClick={() => table.setPageIndex(0)}
45+
disabled={!table.getCanPreviousPage()}
46+
>
47+
<span className="sr-only">Go to first page</span>
48+
<ChevronsLeft className="h-4 w-4" />
49+
</Button>
50+
<Button
51+
variant="outline"
52+
className="h-8 w-8 p-0"
53+
onClick={() => table.previousPage()}
54+
disabled={!table.getCanPreviousPage()}
55+
>
56+
<span className="sr-only">Go to previous page</span>
57+
<ChevronLeftIcon className="h-4 w-4" />
58+
</Button>
59+
<Button
60+
variant="outline"
61+
className="h-8 w-8 p-0"
62+
onClick={() => table.nextPage()}
63+
disabled={!table.getCanNextPage()}
64+
>
65+
<span className="sr-only">Go to next page</span>
66+
<ChevronRightIcon className="h-4 w-4" />
67+
</Button>
68+
<Button
69+
variant="outline"
70+
className="hidden h-8 w-8 p-0 lg:flex"
71+
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
72+
disabled={!table.getCanNextPage()}
73+
>
74+
<span className="sr-only">Go to last page</span>
75+
<ChevronsRight className="h-4 w-4" />
76+
</Button>
77+
</div>
78+
</div>
79+
</div>
80+
);
81+
}

0 commit comments

Comments
 (0)