DataTable
A data table displays structured data in rows and columns, so users can scan, compare and act on it. Use it when the data needs sorting, search, filtering, selection or pagination. It is built on TanStack Table.
Usage
Turn on the features you need with the props below. Define the columns, the data and the row actions outside the component, or memoize them, so they keep the same reference between renders.
| Navn | Rolle | Avdeling | Status | Timer |
|---|---|---|---|---|
| Ola Nordmann | Prosjektleder | Prosjekt | Aktiv | 37.5 |
| Kari Nilsen | Ingeniør | Teknikk | Ferdig | 40 |
| Per Hansen | Kontrollør | Kvalitet | Venter | 20 |
| Mona Berg | Arkitekt | Prosjekt | Aktiv | 32 |
| Jonas Eide | Ingeniør | Teknikk | Aktiv | 40 |
| Silje Aas | Kontrollør | Kvalitet | Ferdig | 24 |
const statusVariant = {
Aktiv: 'info',
Venter: 'warning',
Ferdig: 'success',
};
const columnHelper = createDataTableColumnHelper<Person>();
const columns = [
columnHelper.accessor('navn', { header: 'Navn' }),
columnHelper.accessor('rolle', { header: 'Rolle' }),
columnHelper.accessor('avdeling', { header: 'Avdeling' }),
columnHelper.accessor('status', {
header: 'Status',
cell: (cell) => (
<Pill size="sm" variant={statusVariant[cell.getValue()]}>
{cell.getValue()}
</Pill>
),
}),
columnHelper.accessor('timer', { header: 'Timer' }),
];
const rowActions: Array<DataTableRowAction<Person>> = [
{
key: 'view',
label: 'Vis detaljer',
icon: <IconEye size={IconSize.sm} />,
onClick: (row) => openDetails(row),
},
{
key: 'edit',
label: 'Rediger',
icon: <IconPencil size={IconSize.sm} />,
onClick: (row) => openEditor(row),
disabled: (row) => row.status === 'Ferdig',
},
{
key: 'delete',
label: 'Slett',
icon: <IconTrash size={IconSize.sm} />,
onClick: (row) => confirmDelete(row),
destructive: true,
},
];
<DataTable
columns={columns}
data={people}
withSorting={false}
withGlobalSearch={false}
withColumnFilters={false}
withRowSelection={false}
withContextMenu={false}
rowActions={rowActions}
withPagination={false}
size="sm"
/>
There is no click handler for a whole row. Open a row from a control inside it. When the row has its own page, link to it from the primary cell:
const columns = [
columnHelper.accessor('name', {
header: 'Navn',
cell: (ctx) => (
<Anchor component={Link} to={`/oppgaver/${ctx.row.original.id}`}>
{ctx.getValue()}
</Anchor>
),
}),
// …
];
When it has no page, add an action to the row menu instead:
const rowActions = [
{
key: 'open',
label: 'Åpne',
icon: <IconArrowRight size={IconSize.sm} />,
onClick: (task: Task) => openDrawer(task.id),
},
// …
];
<DataTable
columns={columns}
data={tasks}
withContextMenu
rowActions={rowActions}
getRowLabel={(task) => task.name}
/>;
Props
Data
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
columns* | Array<DataTableColumn<TData>> | — | — | Column definitions. See Column options below. |
data* | TData[] | — | — | The rows. |
* Required
Column options
Set per column in meta, on columns built with createDataTableColumnHelper.
Text that does not fit is cut off with an ellipsis. A column with its own
cell renderer is never cut off and gets no search highlight.
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
align | string | left · center · right | — | Aligns the header and the cells. |
wrap | boolean | — | false | Wraps long text onto several lines instead of cutting it off. |
tooltip | boolean | — | false | Shows the full value in a tooltip when the text is cut off. |
filterLabel | string | — | — | Names the filter field, when it differs from the header. |
Sorting, search, filtering, visibility
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
withSorting | boolean | — | true | Makes the headers sortable. Turn it off for one column with enableSorting: false. |
withGlobalSearch | boolean | — | false | Adds a search field that searches all columns. Exclude a column with enableGlobalFilter: false. |
searchLabel | string | — | 'Søk i tabellen' | Accessible name of the search field. |
searchPlaceholder | string | — | 'Søk …' | Placeholder of the search field. |
withColumnFilters | boolean | — | false | Adds a filter field under each header. Exclude a column with enableColumnFilter: false. |
withColumnVisibility | boolean | — | false | Adds a menu for hiding columns. Exclude a column with enableHiding: false. |
columnsLabel | string | — | 'Kolonner' | Label of the column menu button. |
withRowSelection | boolean | — | false | Adds a checkbox to each row. The header checkbox selects the rows currently shown. |
selectAllLabel | string | — | 'Velg alle rader' | Accessible name of the header checkbox. |
selectRowLabel | string | — | 'Velg rad' | Accessible name of a row checkbox when getRowLabel is not set. |
highlightMatches | boolean | — | true | Highlights search matches in plain text cells. |
Pagination and state
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
withPagination | boolean | — | false | Adds page controls and a row count below the table. |
pageSize | number | — | 20 | Rows per page. |
loading | boolean | — | false | Shows a loader in place of the rows. |
error | Error | null | — | — | Shows the error message in place of the rows. |
emptyMessage | string | — | 'Ingen rader' | Shown when there are no rows. |
Row interaction
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
getRowLabel | (row: TData) => string | — | — | Names each row for its checkbox and menu button. |
getRowSelectionLabel | (rowLabel: string) => string | — | 'Velg <row>' | Formats the checkbox name when getRowLabel is set. |
getRowActionsLabel | (rowLabel: string) => string | — | 'Handlinger for <row>' | Formats the menu button name when getRowLabel is set. |
withContextMenu | boolean | — | false | Adds a menu button to each row. Requires rowActions. |
rowActions | Array<DataTableRowAction<TData>> | — | — | The actions in the row menu. See Row action fields below. |
rowActionsLabel | string | — | 'Radhandlinger' | Accessible name of the menu button when getRowLabel is not set. |
The column ids select and actions are reserved.
Row action fields
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
key* | string | — | — | Unique id. |
label* | ReactNode | — | — | Menu item text. |
onClick* | (row: TData) => void | — | — | Runs with the row the menu belongs to. |
icon | ReactNode | — | — | Icon before the label. |
disabled | (row: TData) => boolean | — | — | Disables the action for that row. |
hidden | (row: TData) => boolean | — | — | Hides the action for that row. A row with no visible actions gets no menu button. |
destructive | boolean | — | false | Shows the action in red, with a divider above it. |
* Required
Appearance and layout
| Property | Type | Allowed values | Default | Description |
|---|---|---|---|---|
radius | MantineRadius | — | sm | Rounds the frame around the table. |
size | DataTableSize | sm · md · lg | sm | Sets the minimum row height. |
withTableBorder | boolean | — | false | Adds an inner border. The table already has a frame. |
striped / withRowBorders / withColumnBorders | MantineTableProps['striped'] / boolean / boolean | — | — | Same as on Table. |
tabularNums / layout | boolean / MantineTableProps['layout'] | — | — | Same as on Table. |
stickyHeader / stickyHeaderOffset | boolean / number | string | — | — | Same as on Table. |
horizontalSpacing / verticalSpacing | MantineSpacing | — | smd / xs | Cell padding. |
minWidth / maxHeight | number | string | — | — | Makes the table scroll horizontally or vertically past this size. |
Accessibility
- Set
getRowLabelwhen rows can be selected or have a menu, so each checkbox and menu button names its row. - Set
searchLabelto name what is searched, for example "Søk i oppgaver". - Rows are not clickable. Put the interaction on a control inside the row, as shown under Usage.
See Accessibility for the reasoning.