Data Table
Builds a table with sorting, filtering, pagination, row selection and column visibility from the Table component and TanStack Table.
| Order | Supplier | Site | Status | Total | |
|---|---|---|---|---|---|
| PO-2417 | Bullovia | Bergamo | Awaiting approval | €18,420 | |
| PO-2416 | Ferrolva Acciai | Brescia | In transit | €52,300 | |
| PO-2415 | Plastrena | Padova | Received | €7,860 | |
| PO-2414 | Cuscinal | Bologna | Received | €12,950 | |
| PO-2413 | Imbaltra | Bergamo | In transit | €3,180 |
0 of 12 orders selected
Installation
Data Table is a composition, not a registry item. There is no file to copy: you add the components it is built from and the headless table library.
pnpm dlx shadcn@latest add @silkware/table @silkware/button @silkware/checkbox @silkware/dropdown-menu @silkware/inputThen add TanStack Table. This page uses version 9, which is feature based:
pnpm add @tanstack/react-tableAdd only the components your table uses. A table that only sorts needs table and @tanstack/react-table.
Usage
import { createColumnHelper, tableFeatures, useTable,} from "@tanstack/react-table"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"TanStack Table holds the state and computes the rows. The Table renders them and handles focus, selection and sort interaction. Wire the two together in four steps: declare the features, define the columns, call useTable, and render the rows.
const features = tableFeatures({})
const columnHelper = createColumnHelper<typeof features, Order>()
const columns = columnHelper.columns([ columnHelper.accessor("id", { header: "Order" }), columnHelper.accessor("supplier", { header: "Supplier" }),])
function OrdersTable({ orders }: { orders: Order[] }) { const table = useTable({ features, data: orders, columns })
return ( <Table aria-label="Purchase orders"> <TableHeader> {table.getFlatHeaders().map((header) => ( <TableHead key={header.id} id={header.id} isRowHeader={header.index === 0}> {header.isPlaceholder ? null : <table.FlexRender header={header} />} </TableHead> ))} </TableHeader> <TableBody> {table.getRowModel().rows.map((row) => ( <TableRow key={row.id} id={row.id}> {row.getAllCells().map((cell) => ( <TableCell key={cell.id}> <table.FlexRender cell={cell} /> </TableCell> ))} </TableRow> ))} </TableBody> </Table> )}Three things people get wrong:
- Version 9 only includes what you register. Sorting, filtering, pagination, selection and visibility each need their feature in
tableFeatures, and sorting, filtering and pagination need their row model too. Without the visibility feature,row.getVisibleCells()does not exist: userow.getAllCells(). - Pass
idto everyTableHeadandTableRow, and passgetRowIdtouseTableso a row keeps its identity when the data is sorted or filtered. - The component that calls
useTableholds state and passes functions, so its file starts with"use client".
Examples
Basic
A table with columns defined in one place and cells formatted per column. Status uses the brand's state square next to a word.
| Order | Supplier | Site | Status | Total |
|---|---|---|---|---|
| PO-2417 | Bullovia | Bergamo | Awaiting approval | €18,420 |
| PO-2416 | Ferrolva Acciai | Brescia | In transit | €52,300 |
| PO-2415 | Plastrena | Padova | Received | €7,860 |
| PO-2414 | Cuscinal | Bologna | Received | €12,950 |
Sorting
Register rowSortingFeature and createSortedRowModel(), keep sorting in state, and translate it to the Table's sortDescriptor. A header that can be sorted gets allowsSorting; the Table calls onSortChange when it is pressed.
| Order | Supplier | Total |
|---|---|---|
| PO-2416 | Ferrolva Acciai | €52,300 |
| PO-2417 | Bullovia | €18,420 |
| PO-2414 | Cuscinal | €12,950 |
| PO-2415 | Plastrena | €7,860 |
| PO-2413 | Imbaltra | €3,180 |
Filtering
Register columnFilteringFeature and createFilteredRowModel() with a filter function. The input sets the filter value of one column. It has an aria-label because it has no visible label. An empty result shows the Table's empty state.
| Order | Supplier | Site |
|---|---|---|
| PO-2416 | Ferrolva Acciai | Brescia |
Pagination
Register rowPaginationFeature and createPaginatedRowModel(). Pages hold 10 rows unless you set pageSize in the pagination state. Say where the reader is: "Orders 1 to 5 of 12", not only Previous and Next.
| Order | Supplier | Total |
|---|---|---|
| PO-2417 | Bullovia | €18,420 |
| PO-2416 | Ferrolva Acciai | €52,300 |
| PO-2415 | Plastrena | €7,860 |
| PO-2414 | Cuscinal | €12,950 |
| PO-2413 | Imbaltra | €3,180 |
Orders 1 to 5 of 12
Row selection
Register rowSelectionFeature and set selectionMode="multiple" on the Table. A Checkbox with slot="selection" goes in a display column, in the header and in each cell. Map the Table's selection to setRowSelection, and read the result with getSelectedRowModel().
| Order | Supplier | Total | |
|---|---|---|---|
| PO-2417 | Bullovia | €18,420 | |
| PO-2416 | Ferrolva Acciai | €52,300 | |
| PO-2415 | Plastrena | €7,860 | |
| PO-2414 | Cuscinal | €12,950 |
1 of 4 orders selected
Column visibility
Register columnVisibilityFeature. A Dropdown Menu group in multiple selection mode lists the columns that may be hidden. Set enableHiding: false on a column that must stay, such as the order number.
| Order | Supplier | Total |
|---|---|---|
| PO-2417 | Bullovia | €18,420 |
| PO-2416 | Ferrolva Acciai | €52,300 |
| PO-2415 | Plastrena | €7,860 |
| PO-2414 | Cuscinal | €12,950 |
Everything together
The first example on this page combines all five. Start from the smallest example that covers your case and add features one at a time.
Accessibility
Keyboard behaviour is the Table's. See Table for the keys. The additions in a data table are the controls around it.
| Key | Action |
|---|---|
| Space | Sorts by the focused column header, or toggles the focused row's checkbox. |
| Tab | Moves focus between the filter, the columns menu, the table and the page buttons. |
| Enter | Sorts by the focused column header when it can be sorted, opens the columns menu from its button, or presses a page button. |
| ArrowDown | Moves focus to the next item in the columns menu, or to the cell below in the table. |
| Escape | Closes the columns menu and returns focus to its button. |
React Aria provides:
- Grid navigation, sort state with
aria-sort, and selection announcements, through the Table. - Press handling, focus and disabled state on the page buttons and the columns menu.
TanStack Table provides:
- The state and the row models. It renders nothing and knows nothing about accessibility.
You provide:
- An
aria-labelon the Table and on every input that has no visible label, and a name on each checkbox, such as "Select PO-2417". - The position in the data, for example "Orders 6 to 10 of 48", in text.
- A message when the filter removes every row.
- A sort that you can also reach without the header, if sorting matters to the task.
Guidelines
When to use
- For tens to thousands of records that people sort, filter, select and page through: orders, suppliers, quality checks.
- When the same view is used every day and the toolbar earns its place.
When not to use
- To show a few records with no interaction. Use a plain Table.
- For a list of titled entries with an action each. Use an Item group.
- For records with many fields per row on a phone. Use a list of Cards or Items and keep the table for wide screens.
- When the server should do the work. For large data, filter, sort and page on the server and pass the result in. TanStack Table supports this with manual modes, which this page does not cover.
Content
- Offer the filters people use most. One input for the main field beats five dropdowns.
- Say how many rows are selected and how many exist.
- Keep the toolbar to one line: filter on the left, the columns menu on the right.
- Hide columns by default only when the table is too wide, and say in the columns menu what is hidden.
Pagination
Orders 6 to 10 of 48
Show the range and the total so the reader knows where they are.
Don’t leave Previous and Next alone. Nobody can tell how much is left.
API reference
Data Table has no props of its own: it is the Table with @tanstack/react-table. The calls you use most are below. They are from version 9.3.
tableFeatures
Declares the features of a table and registers their row models. Pass the result as features to useTable and as the first type argument to createColumnHelper.
| Prop | Type | Default | Description |
|---|---|---|---|
rowSortingFeature? | feature | None | Adds sorting state and column sorting. Needs sortedRowModel and sortFns. |
columnFilteringFeature? | feature | None | Adds column filters. Needs filteredRowModel and filterFns. |
rowPaginationFeature? | feature | None | Adds page state and page navigation. Needs paginatedRowModel. |
rowSelectionFeature? | feature | None | Adds row selection state. |
columnVisibilityFeature? | feature | None | Adds column visibility state and getVisibleCells on rows. |
useTable
Creates the table instance. Keep state in React and pass it back with state and the matching on...Change handler.
| Prop | Type | Default | Description |
|---|---|---|---|
features | TableFeatures | None | The result of tableFeatures. |
data | TData[] | None | The rows. Keep the array stable between renders. |
columns | ColumnDef[] | None | The result of columnHelper.columns. |
getRowId? | (row: TData) => string | None | Returns a stable id for a row. Defaults to the row index. |
state? | object | None | The controlled slices: sorting, columnFilters, columnVisibility, rowSelection, pagination. |
onSortingChange? | Updater | None | Called with the next sorting state. |
onColumnFiltersChange? | Updater | None | Called with the next filters. |
onColumnVisibilityChange? | Updater | None | Called with the next visibility state. |
onRowSelectionChange? | Updater | None | Called with the next selection. |
onPaginationChange? | Updater | None | Called with the next page index and size. |
Table instance
The methods used on this page: getFlatHeaders(), getRowModel(), getColumn(id), setSorting, previousPage(), nextPage(), getCanPreviousPage(), getCanNextPage(), getPageCount(), getSelectedRowModel(), getFilteredRowModel(), setRowSelection, toggleAllRowsSelected(), setColumnVisibility, getAllColumns() and getVisibleFlatColumns(). table.FlexRender renders a header or a cell. See the TanStack Table docs for the full API.