Data Table
A client-side data table built on TanStack Table with sorting, filtering, pagination, column visibility, pinning, resizing and expandable rows.
A client-side table on TanStack Table v9: sort, search, faceted filters, column visibility, pinning, resizing, expandable rows and pagination. Each part lives in its own file under components/ui-fragments/data-table/, so a custom table can reuse the toolbar and the content with its own useTable.
Installation#
npx shadcn@latest add @formance/data-tableInstall the following dependencies:
npm install @tanstack/react-table lucide-reactCopy the files of registry/default/ui-fragments/data-table/ into
components/ui-fragments/data-table/.
Update the import paths to match your project setup.
Usage#
Import each part from its own file:
import { DataTable } from '@/components/ui-fragments/data-table/data-table';import { DataTableColumnHeader } from '@/components/ui-fragments/data-table/data-table-column-header';import type { ColumnDef } from '@/components/ui-fragments/data-table/data-table-features';import { DataTableRowActions } from '@/components/ui-fragments/data-table/data-table-row-actions';import { DataTableSkeleton } from '@/components/ui-fragments/data-table/data-table-skeleton';<DataTable columns={columns} data={data} initialState={{ sorting: [{ id: 'amount', desc: true }] }} searchConfig={[{ placeholder: 'Filter emails...', columnKey: 'email' }]}/>Pinned and expanded#
Seed initialState.columnPinning to keep columns in place while the rest scrolls. Add createExpandColumn() and renderExpandedRow to show details under a row.
Table options#
tableOptions passes any other TanStack option to useTable. Row selection is on and cell selection is off by default; turn either one around, control a state slice, or listen to a change:
<DataTable columns={columns} data={data} tableOptions={{ enableRowSelection: (row) => row.original.status !== 'locked', onRowSelectionChange: setRowSelection, state: { rowSelection }, }}/>The table keeps features, data and columns, and takes initialState, getRowId, getRowCanExpand, autoResetPageIndex and autoResetExpanded from their own props.
Row selection#
Control the rowSelection slice through tableOptions to read or clear the selection from the page. A function in enableRowSelection decides which rows a person can select; the select column reads it with row.getCanSelect().
External controls#
onTableReady hands the table over once, on mount. Its methods read the live state, so controls outside the table can search, sort or reset it. hideToolbar removes the built-in toolbar.
Cell selection#
Set enableCellSelection in tableOptions. Drag to select a range, hold Shift to extend it, hold Ctrl or Cmd to add or remove a range. Read the result with table.getSelectedCellCount() or table.getSelectedCellRangesData().
Skeleton#
DataTableSkeleton holds the place of the table while the data loads.
Build your own table#
DataTable is useTable with dataTableFeatures, then DataTableToolbar, DataTableContent and DataTablePagination. To change one part (a server-side pager, say), build the table yourself with the same feature set, so your ColumnDef types stay the same:
const table = useTable({ features: dataTableFeatures, data, columns });
return ( <div className="space-y-4"> <DataTableToolbar table={table} searchConfig={searchConfig} /> <DataTableContent table={table} /> <MyPager table={table} /> </div>);Cursor pager#
A table for an API that answers one page at a time: useTable with dataTableFeatures and the manual flags, DataTableToolbar and DataTableContent, then a pager built on DataTablePageSizeSelect that walks the cursors.
Compound Components
Compose the pieces you need:
<DataTable />Client-side table: toolbar, content and pagination over one useTable.<DataTableContent />The table itself: header, rows, resize handles, pinned columns, expanded rows and the empty placeholder. Render it with your own useTable to build a custom table.<DataTableColumnHeader />Sortable column header with dropdown sort controls.<DataTablePagination />Pagination controls with page size selector.<DataTableToolbar />Toolbar with search, faceted filters, and view options.<DataTableViewOptions />Column visibility toggle dropdown.<DataTableFacetedFilter />Faceted filter popover for column filtering.<DataTableRowActions />Row-level action dropdown menu.<DataTableSkeleton />Loading skeleton placeholder for the data table.