Shadcn React Table

Search documentation

Search the docs

Density

Density controls the vertical padding of table rows. The data table ships three densities: "comfortable" (the default), "compact", and "spacious". A toolbar button cycles through them, so end users can pick the row height that suits them.

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

Usage

Set the initial density with defaultDensity:

const table = useDataTable({
  data,
  columns,
  getRowId: (row) => row.id,
  defaultDensity: "compact",
})

return <DataTable table={table} />

Reading and setting density

You can read or change the current density at any time through the table instance:

// current density: "comfortable" | "compact" | "spacious"
const density = table.cnTable.density

// switch to compact programmatically
table.cnTable.setDensity("compact")

This is handy when you want to drive density from your own controls instead of the built-in toolbar button.

Hiding the toolbar button

Each toolbar control can be hidden individually. Pass enableDensityToggle: false to drop the density button (for example when you drive density from your own UI), or enableFullscreenToggle: false to drop the full-screen button:

const table = useDataTable({
  data,
  columns,
  getRowId: (row) => row.id,
  enableDensityToggle: false,
  enableFullscreenToggle: false,
})

The other toolbar options have their own flags too: enableGlobalFilter, enableColumnFilters, enableColumnActions, and enableExport. To remove the entire top toolbar, use enableTopToolbar: false.

Persisting density

density is plain state on the table instance, so you can mirror it into local storage or a query param and restore it via defaultDensity on the next load.