Skip to content

Data Table

Builds a table with sorting, filtering, pagination, row selection and column visibility from the Table component and TanStack Table.

OrderSupplierSiteStatusTotal
PO-2417BulloviaBergamoAwaiting approval
€18,420
PO-2416Ferrolva AcciaiBresciaIn transit
€52,300
PO-2415PlastrenaPadovaReceived
€7,860
PO-2414CuscinalBolognaReceived
€12,950
PO-2413ImbaltraBergamoIn transit
€3,180

0 of 12 orders selected

Page 1 of 3

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/input

Then add TanStack Table. This page uses version 9, which is feature based:

pnpm add @tanstack/react-table

Add 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: use row.getAllCells().
  • Pass id to every TableHead and TableRow, and pass getRowId to useTable so a row keeps its identity when the data is sorted or filtered.
  • The component that calls useTable holds 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.

OrderSupplierSiteStatus
Total
PO-2417BulloviaBergamoAwaiting approval
€18,420
PO-2416Ferrolva AcciaiBresciaIn transit
€52,300
PO-2415PlastrenaPadovaReceived
€7,860
PO-2414CuscinalBolognaReceived
€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.

OrderSupplierTotal
PO-2416Ferrolva Acciai
€52,300
PO-2417Bullovia
€18,420
PO-2414Cuscinal
€12,950
PO-2415Plastrena
€7,860
PO-2413Imbaltra
€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.

OrderSupplierSite
PO-2416Ferrolva AcciaiBrescia

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.

OrderSupplier
Total
PO-2417Bullovia
€18,420
PO-2416Ferrolva Acciai
€52,300
PO-2415Plastrena
€7,860
PO-2414Cuscinal
€12,950
PO-2413Imbaltra
€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().

OrderSupplier
Total
PO-2417Bullovia
€18,420
PO-2416Ferrolva Acciai
€52,300
PO-2415Plastrena
€7,860
PO-2414Cuscinal
€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.

OrderSupplier
Total
PO-2417Bullovia
€18,420
PO-2416Ferrolva Acciai
€52,300
PO-2415Plastrena
€7,860
PO-2414Cuscinal
€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.

KeyAction
SpaceSorts by the focused column header, or toggles the focused row's checkbox.
TabMoves focus between the filter, the columns menu, the table and the page buttons.
EnterSorts by the focused column header when it can be sorted, opens the columns menu from its button, or presses a page button.
ArrowDownMoves focus to the next item in the columns menu, or to the cell below in the table.
EscapeCloses 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-label on 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

Do

Show the range and the total so the reader knows where they are.

Don’t

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.

PropTypeDefaultDescription
rowSortingFeature?featureNoneAdds sorting state and column sorting. Needs sortedRowModel and sortFns.
columnFilteringFeature?featureNoneAdds column filters. Needs filteredRowModel and filterFns.
rowPaginationFeature?featureNoneAdds page state and page navigation. Needs paginatedRowModel.
rowSelectionFeature?featureNoneAdds row selection state.
columnVisibilityFeature?featureNoneAdds 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.

PropTypeDefaultDescription
featuresTableFeaturesNoneThe result of tableFeatures.
dataTData[]NoneThe rows. Keep the array stable between renders.
columnsColumnDef[]NoneThe result of columnHelper.columns.
getRowId?(row: TData) => stringNoneReturns a stable id for a row. Defaults to the row index.
state?objectNoneThe controlled slices: sorting, columnFilters, columnVisibility, rowSelection, pagination.
onSortingChange?UpdaterNoneCalled with the next sorting state.
onColumnFiltersChange?UpdaterNoneCalled with the next filters.
onColumnVisibilityChange?UpdaterNoneCalled with the next visibility state.
onRowSelectionChange?UpdaterNoneCalled with the next selection.
onPaginationChange?UpdaterNoneCalled 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.