Data Table
A table you can use as it is, built on TanStack Table: sortable columns, row selection, search, a column menu, pagination, and loading and empty states. Define columns the way shadcn's data table examples do.
Install
npx shadcn@latest add @nelumbo/data-tableTeam
Click a header to sort (again to reverse, once more to clear). Tick rows to get the selection bar; the select-all box shows a dash when only some of the page is selected. Search nonsense such as “zzz” to see the empty state, and use the switches to try loading and compact rows.
| Actions | ||||||
|---|---|---|---|---|---|---|
Maya Kowalski | maya.kowalski@nelumbo.test | Designer | $250.00 | |||
Kenji Watanabe | kenji.watanabe@nelumbo.test | Engineer | $8,169.37 | |||
Léa Martin | lea.martin@nelumbo.test | Engineer | $7,088.74 | |||
Omar Haddad | omar.haddad@nelumbo.test | Engineer | $6,008.11 | |||
Sofía Ramírez | sofia.ramirez@nelumbo.test | Engineer | $4,927.48 | |||
Tomás Novák | tomas.novak@nelumbo.test | Researcher | $3,846.85 | |||
Aisha Bello | aisha.bello@nelumbo.test | Writer | $2,766.22 | |||
Ravi Iyer | ravi.iyer@nelumbo.test | Analyst | $1,683.00 | |||
Noor Bello | noor.bello@nelumbo.test | Designer | $602.37 | |||
Elif Nair | elif.nair@nelumbo.test | Product manager | $8,521.74 |
Rows per page
1–10 of 48
Empty
With no rows, the table shows an empty state. Pass emptyState for your own.
Nothing here yet There are no rows to show. | |||
Usage
import type { ColumnDef } from "@tanstack/react-table"import { DataTable, DataTableColumnHeader } from "@/components/ui/data-table"type Member = { id: string; name: string; email: string; amount: number }const columns: ColumnDef<Member>[] = [ { accessorKey: "name", header: ({ column }) => <DataTableColumnHeader column={column} title="Name" />, }, { accessorKey: "email", header: "Email", enableSorting: false }, { accessorKey: "amount", meta: { align: "end" }, header: ({ column }) => <DataTableColumnHeader column={column} title="Amount" />, cell: ({ row }) => <span className="tabular-nums">{money.format(row.original.amount)}</span>, },]<DataTable aria-label="Team members" columns={columns} data={members} getRowId={(m) => m.id} selectable getRowLabel={(m) => m.name} loading={isLoading} bulkActions={({ rows, clear }) => ( <Button size="sm" variant="ghost" onClick={() => { remove(rows); clear() }}>Remove</Button> )}/>// Define columns outside the component (or memoize them), like any TanStack table.// Format numbers and dates with a fixed locale so server and client agree.Props
| Prop | Type | Default | Notes |
|---|---|---|---|
columns | ColumnDef<TData>[] | TanStack column definitions | |
data | TData[] | ||
getRowId | (row, index) => string | index | Stable ids keep selection through sorting and new data |
selectable / getRowLabel | boolean / (row) => string | false | Checkbox column; getRowLabel names each box |
onSelectionChange | (rows) => void | Selected rows that match the search | |
bulkActions | ({ rows, table, clear }) => ReactNode | Buttons shown in the selection bar | |
search / searchPlaceholder | boolean / string | true / "Search" | Global filter over every column |
columnMenu | boolean | true | The Columns menu |
toolbar | ReactNode | Extra toolbar content, before the Columns menu | |
pagination / pageSize / pageSizeOptions | boolean / number / number[] | true / 10 / [5, 10, 20, 50] | |
initialSorting / initialColumnVisibility | SortingState / VisibilityState | ||
loading | boolean | false | Skeleton rows, aria-busy |
emptyState | ReactNode | Replaces the default empty state | |
density / stickyHeader | "comfortable" / false | As Table | |
column meta | { align, label, className, headerClassName } | Alignment, menu name and cell classes | |
DataTableColumnHeader | { column, title } | Sort button for a header; the cell gets aria-sort | |
DataTablePagination, DataTableViewOptions, selectColumn | The parts, for tables you wire up yourself |