Getting started

Get started with @mantine/data-grid package

License

Installation

Install @mantine/data-grid and its peer dependencies:

yarn add @mantine/data-grid @tanstack/react-table @tanstack/react-virtual @tanstack/match-sorter-utils

Import the package styles after @mantine/core styles:

import '@mantine/core/styles.css';
import '@mantine/data-grid/styles.css';

Quick start

import { DataGrid } from '@mantine/data-grid';

const data = [
  { id: '1', name: 'Alice', age: 30 },
  { id: '2', name: 'Bob', age: 25 },
];

function Demo() {
  return (
    <DataGrid
      data={data}
      columns={[
        { accessorKey: 'name', header: 'Name' },
        { accessorKey: 'age', header: 'Age' },
      ]}
    />
  );
}

Store hook

Use useDataGrid to create a store that manages grid state and pass it to DataGrid with the store prop:

import { DataGrid, useDataGrid } from '@mantine/data-grid';

function Demo() {
  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'name', header: 'Name', enableSorting: true },
      { accessorKey: 'age', header: 'Age' },
    ],
  });

  return <DataGrid store={store} />;
}

createDataGridColumnHelper

createDataGridColumnHelper is a wrapper around TanStack Table createColumnHelper. Pass your row data type as a generic to get autocompletion on accessor keys and cell values:

import { createDataGridColumnHelper, DataGrid, useDataGrid } from '@mantine/data-grid';

const columnHelper = createDataGridColumnHelper<Person>();

const columns = [
  columnHelper.accessor('name', { header: 'Name', enableSorting: true }),
  columnHelper.accessor('age', { header: 'Age' }),
  columnHelper.display({
    id: 'actions',
    meta: { type: 'actions' },
    cell: () => <Button size="xs">Edit</Button>,
  }),
];

Column definition types

TanStack Table supports three kinds of column definitions:

  • Accessor columns – defined with accessorKey or accessorFn. Read data from the row and support sorting, filtering and grouping.
  • Display columns – defined with an id and no accessor. Used for action buttons, checkboxes or any content not tied to row data.
  • Group columns – defined with a columns array. Create a multi-level header by nesting other columns inside a group.

defaultColumn

Pass defaultColumn in tableOptions to set default values of column properties:

const store = useDataGrid({
  data,
  columns,
  tableOptions: {
    defaultColumn: { size: 200, minSize: 50, maxSize: 500 },
  },
});

Features overview

Features are configured with useDataGrid options. Most of them are enabled automatically when you pass controlled state or column-level flags:

FeatureInferred from
SortingColumns with enableSorting: true
Column filteringColumns with enableColumnFilter or filterFn
Global filterglobalFilter or onGlobalFilterChange
Paginationpagination, onPaginationChange, pageSize, pageCount, rowCount or manualPagination
Column resizingcolumnSizing, onColumnSizingChange or columnResizeMode
Column pinningcolumnPinning or onColumnPinningChange
Column visibilitycolumnVisibility or onColumnVisibilityChange
Column reordercolumnOrder or onColumnOrderChange
Row selectionrowSelection or onRowSelectionChange
Row expansionexpanded, onExpandedChange or getSubRows
Groupinggrouping or onGroupingChange
Row pinningrowPinning, onRowPinningChange or enableRowPinning
Tree datagetSubRows
Row reorderonRowOrderChange
Cell selectioncellSelection or onCellSelectionChange
EditingeditingMode is set

Set the matching with* option (for example, withSorting) to force a feature on or off. Appearance props such as striped or withColumnBorders are set on the DataGrid component, see Customization.

Peer dependencies

@mantine/data-grid has the following peer dependencies:

PackageMin versionPurpose
@tanstack/react-table>=9.0.0Headless table logic
@tanstack/match-sorter-utils>=9.0.0Fuzzy filtering and ranking
@tanstack/react-virtual>=3.13.0Row virtualization
@mantine/core9.0.2UI components
@mantine/hooks9.0.2React hooks