import { Button } from '@/components/ui/button'; import { Table } from '@tanstack/react-table'; import { ChevronLeft, ChevronRight } from 'lucide-react'; import { useSearchParams } from 'next/navigation'; import { useRouter } from 'next/navigation'; import React from 'react'; interface DataTablePaginationProps { table: Table; totalPage: number; // Total jumlah halaman totalData: number; // Total jumlah data visiblePageCount?: number; // Jumlah halaman yang ditampilkan (default 5) } const TablePagination = ({ table, totalPage, totalData, visiblePageCount = 5 }: DataTablePaginationProps) => { const router = useRouter(); const searchParams = useSearchParams(); const [currentPageIndex, setCurrentPageIndex] = React.useState(1); React.useEffect(() => { const pageFromUrl = searchParams?.get('page'); if (pageFromUrl) { setCurrentPageIndex(Number(pageFromUrl)); } }, [searchParams]); // Hitung startPage dan endPage, disesuaikan untuk dimulai dari 1 const startPage = Math.max(1, currentPageIndex - Math.floor(visiblePageCount / 2)); const endPage = Math.min(totalPage, startPage + visiblePageCount - 1); const handlePageChange = (pageIndex: number) => { // Perbarui query parameter di URL const searchParams = new URLSearchParams(window.location.search); searchParams.set('page', (pageIndex).toString()); // Menyimpan halaman sebagai 1-based index router.push(`${window.location.pathname}?${searchParams.toString()}`); // Pindahkan halaman di tabel table.setPageIndex(pageIndex); setCurrentPageIndex(pageIndex + 1); // Update state untuk halaman saat ini }; return (
{table.getFilteredSelectedRowModel().rows.length} of{" "} {totalData} row(s) selected.
{Array.from({ length: endPage - startPage + 1 }, (_, index) => startPage + index).map((pageIndex) => ( ))}
); }; export default TablePagination;