Column features

DataGrid column reorder and header groups

Column reorder

Pass withColumnReorder to useDataGrid to let users reorder columns by dragging column headers. Use columnOrder and onColumnOrderChange for controlled state.

  • A column can be moved only within its pin region and its header group, utility columns always stay first
  • With withColumnMenu, the Move left and Move right menu items reorder columns from the keyboard
  • Grouped columns stay first while rows are grouped, see groupedColumnMode

Drag column headers to reorder

First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { Text } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'email', header: 'Email', size: 220 },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary' },
    ],
    withColumnReorder: true,
  });

  return (
    <>
      <Text mb="md">Drag column headers to reorder</Text>
      <DataGrid store={store} />
    </>
  );
}

Header groups

Use the columns property of a column definition to group columns under a shared header. Group headers can be resized, but cannot be sorted or dragged:

Name
Details
First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      {
        header: 'Name',
        columns: [
          { accessorKey: 'firstName', header: 'First Name' },
          { accessorKey: 'lastName', header: 'Last Name' },
        ],
      },
      {
        header: 'Details',
        columns: [
          { accessorKey: 'email', header: 'Email', size: 220 },
          { accessorKey: 'department', header: 'Department' },
        ],
      },
      { accessorKey: 'salary', header: 'Salary' },
    ],
  });

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

Column alignment

Set meta.align on a column to align its header, body and footer cells. Columns with align: 'end' use tabular figures, use it for numbers:

const columns = [
  { accessorKey: 'name', header: 'Name' },
  { accessorKey: 'status', header: 'Status', meta: { align: 'center' } },
  { accessorKey: 'amount', header: 'Amount', meta: { align: 'end' } },
];

Utility columns

Row reorder, row selection, row expansion and row numbers features add their own columns at the start of the grid. Utility columns cannot be sorted, filtered, resized or hidden by the user and are not exported. If your code iterates over table.getAllColumns(), skip columns with meta.utility.

Cell spanning

Set spanRows: true on a column definition to merge cells of adjacent rows that have the same value in that column:

Department
First Name
Last Name
Salary
EngineeringAliceJohnson95000
FrankMiller102000
IreneAnderson97000
LeoWhite110000
PatrickMartinez99000
FinanceEvaDavis88000
JamesThomas91000
OliviaGarcia85000
HRDavidBrown65000
KarenJackson63000
QuinnRobinson67000
MarketingBobSmith72000
GraceWilson76000
NathanMartin69000
SalesCarolWilliams68000
HenryTaylor71000
MiaHarris74000
RachelClark73000
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: [...employees].sort((a, b) => a.department.localeCompare(b.department)),
    columns: [
      { accessorKey: 'department', header: 'Department', spanRows: true },
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'salary', header: 'Salary' },
    ],
  });

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

Pass a function instead of true to decide whether a row joins the run anchored at anchorRow:

const columns = [
  {
    accessorKey: 'startDate',
    header: 'Start Date',
    spanRows: ({ anchorValue, value }) =>
      new Date(anchorValue).getFullYear() === new Date(value).getFullYear(),
  },
];

Use spanColumns to make a cell span several columns. Return 1 for no span or Infinity to span the rest of the row:

const columns = [
  {
    accessorKey: 'label',
    header: 'Label',
    spanColumns: ({ row }) => (row.original.isSummary ? Infinity : 1),
  },
];
  • null and undefined values are not merged
  • spanRows and spanColumns are ignored when virtualize is set
  • Set enableCellSpanning: false in tableOptions to disable spanning for the whole grid, or on a column definition to disable it for one column

Related pages