Skip to main content

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.

Base
Wrapper over Mantine
Mantine
Import
{ DataTable } from '@multiconsult-group-products/design-system'
Source
src/components/DataTable/DataTable.tsx
src/components/DataTable/DataTable.features.ts
src/components/DataTable/DataTable.module.css

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.

Sorting
Global Search
Column Filters
Row Selection
Context menu
Pagination
Sub-text
size
sm
md
lg
NavnRolleAvdelingStatusTimer
Ola NordmannProsjektlederProsjektAktiv37.5
Kari NilsenIngeniørTeknikkFerdig40
Per HansenKontrollørKvalitetVenter20
Mona BergArkitektProsjektAktiv32
Jonas EideIngeniørTeknikkAktiv40
Silje AasKontrollørKvalitetFerdig24
React
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:

React
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:

React
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

PropertyTypeAllowed valuesDefaultDescription
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.

PropertyTypeAllowed valuesDefaultDescription
alignstringleft · center · right—Aligns the header and the cells.
wrapboolean—falseWraps long text onto several lines instead of cutting it off.
tooltipboolean—falseShows the full value in a tooltip when the text is cut off.
filterLabelstring——Names the filter field, when it differs from the header.

Sorting, search, filtering, visibility

PropertyTypeAllowed valuesDefaultDescription
withSortingboolean—trueMakes the headers sortable. Turn it off for one column with enableSorting: false.
withGlobalSearchboolean—falseAdds a search field that searches all columns. Exclude a column with enableGlobalFilter: false.
searchLabelstring—'Søk i tabellen'Accessible name of the search field.
searchPlaceholderstring—'Søk …'Placeholder of the search field.
withColumnFiltersboolean—falseAdds a filter field under each header. Exclude a column with enableColumnFilter: false.
withColumnVisibilityboolean—falseAdds a menu for hiding columns. Exclude a column with enableHiding: false.
columnsLabelstring—'Kolonner'Label of the column menu button.
withRowSelectionboolean—falseAdds a checkbox to each row. The header checkbox selects the rows currently shown.
selectAllLabelstring—'Velg alle rader'Accessible name of the header checkbox.
selectRowLabelstring—'Velg rad'Accessible name of a row checkbox when getRowLabel is not set.
highlightMatchesboolean—trueHighlights search matches in plain text cells.

Pagination and state

PropertyTypeAllowed valuesDefaultDescription
withPaginationboolean—falseAdds page controls and a row count below the table.
pageSizenumber—20Rows per page.
loadingboolean—falseShows a loader in place of the rows.
errorError | null——Shows the error message in place of the rows.
emptyMessagestring—'Ingen rader'Shown when there are no rows.

Row interaction

PropertyTypeAllowed valuesDefaultDescription
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.
withContextMenuboolean—falseAdds a menu button to each row. Requires rowActions.
rowActionsArray<DataTableRowAction<TData>>——The actions in the row menu. See Row action fields below.
rowActionsLabelstring—'Radhandlinger'Accessible name of the menu button when getRowLabel is not set.

The column ids select and actions are reserved.

Row action fields

PropertyTypeAllowed valuesDefaultDescription
key*string——Unique id.
label*ReactNode——Menu item text.
onClick*(row: TData) => void——Runs with the row the menu belongs to.
iconReactNode——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.
destructiveboolean—falseShows the action in red, with a divider above it.

* Required

Appearance and layout

PropertyTypeAllowed valuesDefaultDescription
radiusMantineRadius—smRounds the frame around the table.
sizeDataTableSizesm · md · lgsmSets the minimum row height.
withTableBorderboolean—falseAdds an inner border. The table already has a frame.
striped / withRowBorders / withColumnBordersMantineTableProps['striped'] / boolean / boolean——Same as on Table.
tabularNums / layoutboolean / MantineTableProps['layout']——Same as on Table.
stickyHeader / stickyHeaderOffsetboolean / number | string——Same as on Table.
horizontalSpacing / verticalSpacingMantineSpacing—smd / xsCell padding.
minWidth / maxHeightnumber | string——Makes the table scroll horizontally or vertically past this size.

Accessibility​

  • Set getRowLabel when rows can be selected or have a menu, so each checkbox and menu button names its row.
  • Set searchLabel to 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.