diff --git a/resources/js/components/data-table.tsx b/resources/js/components/data-table.tsx index 96d3de9..9f7ee59 100644 --- a/resources/js/components/data-table.tsx +++ b/resources/js/components/data-table.tsx @@ -1,9 +1,9 @@ import type { ColumnDef, ColumnFiltersState, + PaginationState, SortingState, VisibilityState, - PaginationState, } from '@tanstack/react-table'; import { flexRender, @@ -87,6 +87,7 @@ interface DataTableProps { showSelection?: boolean; rowSelection?: any; onRowSelectionChange?: (selection: any) => void; + meta?: any; // PaginatedData from Inertia } export function DataTable({ @@ -98,6 +99,7 @@ export function DataTable({ showSelection = true, rowSelection = {}, onRowSelectionChange, + meta, }: DataTableProps) { const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = @@ -182,6 +184,7 @@ export function DataTable({ }, onPaginationChange: setPagination, autoResetPageIndex: false, + manualPagination: !!meta, }); const selectedRows = table.getFilteredSelectedRowModel().rows; @@ -499,38 +502,50 @@ export function DataTable({
{table.getFilteredSelectedRowModel().rows.length} dari{' '} - {table.getFilteredRowModel().rows.length} baris dipilih. + {meta + ? meta.total + : table.getFilteredRowModel().rows.length}{' '} + baris dipilih.
-
-

Baris per halaman

- -
+ {!meta && ( +
+

+ Baris per halaman +

+ +
+ )}
- {table.getFilteredRowModel().rows.length > 0 ? ( + {meta ? ( + <> + Menampilkan {meta.from || 0} sampai{' '} + {meta.to || 0} dari {meta.total} data + + ) : table.getFilteredRowModel().rows.length > 0 ? ( <> Menampilkan{' '} {table.getState().pagination.pageIndex * @@ -550,46 +565,114 @@ export function DataTable({ 'Tidak ada data' )}
-
- - - - -
+ {meta ? (() => { + const { current_page, last_page, prev_page_url, next_page_url } = meta; + + const leftPages = []; + for (let i = current_page - 3; i < current_page; i++) { + if (i > 0) leftPages.push(i); + } + + const rightPages = []; + for (let i = current_page + 1; i <= current_page + 2; i++) { + if (i <= last_page) rightPages.push(i); + } + + const handlePageChange = (page: number) => { + const url = new URL(window.location.href); + url.searchParams.set('page', page.toString()); + import('@inertiajs/react').then(({ router }) => { + router.get(url.toString(), {}, { preserveState: true, preserveScroll: true }); + }); + }; + + return ( +
+ + + {leftPages.map(p => ( + + ))} + + + + {rightPages.map(p => ( + + ))} + + +
+ ); + })() : ( +
+ + + + +
+ )}
diff --git a/resources/js/types/index.ts b/resources/js/types/index.ts index 5457605..22df541 100644 --- a/resources/js/types/index.ts +++ b/resources/js/types/index.ts @@ -12,3 +12,4 @@ export type * from './system-log'; export type * from './dashboard'; export type * from './role'; export type * from './permission'; +export type * from './pagination'; diff --git a/resources/js/types/pagination.ts b/resources/js/types/pagination.ts new file mode 100644 index 0000000..4d97903 --- /dev/null +++ b/resources/js/types/pagination.ts @@ -0,0 +1,15 @@ +export interface PaginatedData { + current_page: number; + data: T[]; + first_page_url: string; + from: number; + last_page: number; + last_page_url: string; + links: { url: string | null; label: string; active: boolean }[]; + next_page_url: string | null; + path: string; + per_page: number; + prev_page_url: string | null; + to: number; + total: number; +}