--- description: Create an advanced data table with sorting, filtering, and pagination argument-hint: allowed-tools: Read, Write, Bash --- Create a fully-featured data table using TanStack Table and shadcn/ui components. ## Instructions 1. Install required dependencies: - `@tanstack/react-table` - Required shadcn components: `table`, `button`, `input`, `dropdown-menu` 2. Create data table with features: - Column definitions with proper types - Sorting functionality - Filtering (global and column) - Pagination - Row selection - Column visibility toggle - Export functionality (optional) ## Template Structure ```tsx // components/[table-name]/columns.tsx import { ColumnDef } from "@tanstack/react-table" import { Button } from "@/components/ui/button" import { ArrowUpDown, MoreHorizontal } from "lucide-react" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { Checkbox } from "@/components/ui/checkbox" export type [DataType] = { id: string // Define data structure } export const columns: ColumnDef<[DataType]>[] = [ { id: "select", header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} aria-label="Select all" /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label="Select row" /> ), enableSorting: false, enableHiding: false, }, { accessorKey: "field", header: ({ column }) => ( ), cell: ({ row }) =>
{row.getValue("field")}
, }, { id: "actions", cell: ({ row }) => { const item = row.original return ( Actions Edit Delete ) }, }, ] // components/[table-name]/data-table.tsx import { ColumnDef, flexRender, getCoreRowModel, getFilteredRowModel, getPaginationRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { DataTableViewOptions } from "./data-table-view-options" import { DataTablePagination } from "./data-table-pagination" interface DataTableProps { columns: ColumnDef[] data: TData[] searchKey?: string } export function DataTable({ columns, data, searchKey, }: DataTableProps) { const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), getSortedRowModel: getSortedRowModel(), getFilteredRowModel: getFilteredRowModel(), }) return (
{searchKey && ( table.getColumn(searchKey)?.setFilterValue(event.target.value) } className="max-w-sm" /> )}
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext() )} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No results. )}
) } ``` ## Features to Include - **Sorting**: Click column headers to sort - **Filtering**: Global search and column filters - **Pagination**: Navigate through pages - **Selection**: Select individual or all rows - **Column Visibility**: Show/hide columns - **Row Actions**: Edit, delete, view details - **Export**: CSV/Excel export - **Responsive**: Mobile-friendly view ## Example If the user says: `/create-data-table users` 1. Install dependencies: ```bash npm install @tanstack/react-table npx shadcn@latest add table button input dropdown-menu checkbox ``` 2. Create column definitions for users table 3. Create data table component 4. Add pagination component 5. Add column visibility toggle 6. Provide usage example