diff --git a/resources/js/components/data-table.tsx b/resources/js/components/data-table.tsx index 690ba27..6b22d9e 100644 --- a/resources/js/components/data-table.tsx +++ b/resources/js/components/data-table.tsx @@ -19,6 +19,7 @@ import { TableHeader, TableRow, } from "@/components/ui/table"; +import { Checkbox } from "@/components/ui/checkbox"; import { Button } from "./ui/button"; import { DropdownMenu, @@ -34,7 +35,7 @@ import { } from "@/components/ui/dropdown-menu"; import React from "react"; import { Input } from "./ui/input"; -import { Settings2, ChevronDown, ListFilter, Trash2 } from "lucide-react"; +import { Settings2, ChevronDown, ListFilter, Trash2, ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from "lucide-react"; interface DataTableFilterOption { columnId: string; @@ -55,6 +56,8 @@ interface DataTableProps { searchKey?: string; filters?: DataTableFilterOption[]; bulkActions?: DataTableBulkAction[]; + showNumbering?: boolean; + showSelection?: boolean; } export function DataTable({ @@ -62,6 +65,8 @@ export function DataTable({ data, filters, bulkActions, + showNumbering = true, + showSelection = true, }: DataTableProps) { const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState([]); @@ -69,9 +74,55 @@ export function DataTable({ const [rowSelection, setRowSelection] = React.useState({}); const [globalFilter, setGlobalFilter] = React.useState(""); + const finalColumns = React.useMemo(() => { + const computedColumns = [...columns]; + + if (showNumbering) { + const numberingColumn: ColumnDef = { + id: "numbering", + header: "No.", + cell: ({ row, table }) => { + const pageIndex = table.getState().pagination.pageIndex; + const pageSize = table.getState().pagination.pageSize; + return pageIndex * pageSize + row.index + 1; + }, + enableHiding: false, + }; + computedColumns.unshift(numberingColumn); + } + + if (showSelection) { + const selectColumn: ColumnDef = { + id: "select", + header: ({ table }) => ( + table.toggleAllPageRowsSelected(!!value)} + aria-label="Select all" + /> + ), + cell: ({ row }) => ( + row.toggleSelected(!!value)} + aria-label="Select row" + /> + ), + enableSorting: false, + enableHiding: false, + }; + computedColumns.unshift(selectColumn); + } + + return computedColumns; + }, [columns, showNumbering, showSelection]); + const table = useReactTable({ data, - columns, + columns: finalColumns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), onSortingChange: setSorting, @@ -214,7 +265,14 @@ export function DataTable({ {headerGroup.headers.map((header) => { return ( - + {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} @@ -229,7 +287,14 @@ export function DataTable({ table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( - + {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} @@ -237,7 +302,7 @@ export function DataTable({ )) ) : ( - + Tidak ada data yang ditemukan. @@ -247,27 +312,60 @@ export function DataTable({
-
+
{table.getFilteredSelectedRowModel().rows.length} dari{" "} {table.getFilteredRowModel().rows.length} baris dipilih.
-
- - +
+
+ {table.getFilteredRowModel().rows.length > 0 ? ( + <> + Menampilkan {table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1} sampai{" "} + {Math.min((table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize, table.getFilteredRowModel().rows.length)}{" "} + dari {table.getFilteredRowModel().rows.length} data + + ) : ( + "Tidak ada data" + )} +
+
+ + + + +