Shadcn React Table

Search documentation

Search the docs

Display columns

Some features add columns of their own: row selection, the expand toggle, row actions, row numbers, row pinning and the row drag handle. Change any of them with displayColumnDefOptions, keyed by the column's id — it takes the same options as your own columns.

ColumnIdAdded by
Selection checkboxescn-selectenableRowSelection
Expand togglecn-expandrenderDetailPanel, getSubRows
Row actionscn-row-actionsrenderRowActions, renderRowActionMenuItems, row / modal editing
Row numberscn-row-numberenableRowNumbers
Row pin buttoncn-row-pinenableRowPinning (without row numbers)
Drag handlecn-row-dragenableRowOrdering
tsx
const table = useDataTable({
  data,
  columns,
  enableRowNumbers: true,
  enableRowSelection: true,
  displayColumnDefOptions: {
    "cn-row-number": {
      header: "#",
      size: 56,
      cell: ({ row }) => <span className="text-muted-foreground">{row.index + 1}</span>,
    },
    "cn-select": { size: 44 },
  },
})

To change all of them at once, use defaultDisplayColumn; per-id options apply on top of it.

tsx
useDataTable({
  data,
  columns,
  defaultDisplayColumn: { enableResizing: false, meta: { align: "center" } },
})

meta is merged with the column's own, so you only list what you change. The column ids are fixed; use them in initialState.columnPinning or columnOrder too.

Memoize for speed

Inline options work, but a new object on every render also re-renders every row. Define them outside your component, or wrap them in useMemo, to skip that.

Display columnsCustomize the row-number and selection columns.
GitHub
#
ID
1AvaThompsonOwnerEngineeringactive22$45,000Jan 2, 2023
0%
2LiamNguyenAdminMarketinginactive25$47,137Jan 13, 2023
9%
3NoahSilvaEditorDesignpending28$49,274Jan 24, 2023
18%
4EmmaCarterViewerSalesactive31$51,411Feb 4, 2023
27%
5OliviaRossiOwnerSupportinactive34$53,548Feb 15, 2023
36%
6WilliamWalkerAdminEngineeringpending37$55,685Feb 26, 2023
45%
7SophiaPatelEditorMarketingactive40$57,822Mar 9, 2023
54%
8JamesMullerViewerDesigninactive43$59,959Mar 20, 2023
63%
9IsabellaParkOwnerSalespending46$62,096Mar 31, 2023
72%
10LucasReyesAdminSupportactive49$64,233Apr 11, 2023
81%
$4,120,536
Rows per page
1–10 of 48