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.
ID | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Ava | Thompson | Owner | Engineering | active | 22 | $45,000 | Jan 2, 2023 | 0% | ||
| Liam | Nguyen | Admin | Marketing | inactive | 25 | $47,137 | Jan 13, 2023 | 9% | ||
| Noah | Silva | Editor | Design | pending | 28 | $49,274 | Jan 24, 2023 | 18% | ||
| Emma | Carter | Viewer | Sales | active | 31 | $51,411 | Feb 4, 2023 | 27% | ||
| Olivia | Rossi | Owner | Support | inactive | 34 | $53,548 | Feb 15, 2023 | 36% | ||
| William | Walker | Admin | Engineering | pending | 37 | $55,685 | Feb 26, 2023 | 45% | ||
| Sophia | Patel | Editor | Marketing | active | 40 | $57,822 | Mar 9, 2023 | 54% | ||
| James | Muller | Viewer | Design | inactive | 43 | $59,959 | Mar 20, 2023 | 63% | ||
| Isabella | Park | Owner | Sales | pending | 46 | $62,096 | Mar 31, 2023 | 72% | ||
| Lucas | Reyes | Admin | Support | active | 49 | $64,233 | Apr 11, 2023 | 81% | ||
| $4,120,536 |
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.