MRT logoMantine React Table

Column Grouping Example

Mantine React Table has a few different UI options and behaviors for displaying grouped columns. See the Column Grouping Guide to learn more.

Grouped Column Mode

State
Gender
First Name
Last Name
Age
Salary
Utah (1)
Male (5)
DanikaRodriguez5731404
AlfonzoAbernathy4053374
AntwanZieme2156638
KathrynLangworth3925720
HayleePrice5759047
Alaska (2)
Male (4)
EloisaKohler3145801
KianHand5681062
MichaleCollier5975197
EldridgeStroman4259594
Female (4)
LoyceSchmidt2976295
AlveraBalistreri2579844
KaydenEmard3598252
DomingoBauch3635159
Arizona (1)
Male (1)

Rows per page

1-20 of 342

import '@mantine/core/styles.css';
import '@mantine/dates/styles.css'; //if using mantine component features
import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
import { useMemo, useState } from 'react';
import {
  MantineReactTable,
  type MRT_ColumnDef,
  useMantineReactTable,
} from 'mantine-react-table';
import { data, type Person } from './makeData';
import { Group, Radio, Stack, Text } from '@mantine/core';

type ColumnMode = 'false' | 'remove' | 'reorder';

const ExampleGrouping = () => {
  const columns = useMemo<MRT_ColumnDef<Person>[]>(
    //column definitions...
    () => [
      {
        header: 'First Name',
        accessorKey: 'firstName',
      },
      {
        header: 'Last Name',
        accessorKey: 'lastName',
      },
      {
        header: 'Age',
        accessorKey: 'age',
      },
      {
        header: 'Gender',
        accessorKey: 'gender',
      },
      {
        header: 'State',
        accessorKey: 'state',
      },
      {
        header: 'Salary',
        accessorKey: 'salary',
      },
    ],
    [],
    //end
  );

  //demo state
  const [groupedColumnMode, setGroupedColumnMode] =
    useState<ColumnMode>('reorder'); //default is 'reorder

  const table = useMantineReactTable({
    columns,
    data,
    enableGrouping: true,
    groupedColumnMode:
      groupedColumnMode === 'false' ? false : groupedColumnMode,
    initialState: {
      expanded: true, //expand all groups by default
      grouping: ['state', 'gender'], //an array of columns to group by default (can be multiple)
      pagination: { pageIndex: 0, pageSize: 20 },
    },
  });

  return (
    <Stack gap="1rem">
      <DemoRadioGroup
        groupedColumnMode={groupedColumnMode}
        setGroupedColumnMode={setGroupedColumnMode}
      />
      <MantineReactTable table={table} />
    </Stack>
  );
};

export default ExampleGrouping;

//demo...
const DemoRadioGroup = ({
  groupedColumnMode,
  setGroupedColumnMode,
}: {
  groupedColumnMode: 'false' | 'remove' | 'reorder';
  setGroupedColumnMode: (
    groupedColumnMode: 'false' | 'remove' | 'reorder',
  ) => void;
}) => {
  return (
    <Stack m={'auto'} align={'center'}>
      <Text>Grouped Column Mode</Text>
      <Radio.Group
        value={groupedColumnMode}
        onChange={(event) => setGroupedColumnMode(event as ColumnMode)}
      >
        <Group>
          <Radio value={'reorder'} label={'Reorder (default)'} />
          <Radio value={'remove'} label={'Remove'} />
          <Radio value={'false'} label={'False'} />
        </Group>
      </Radio.Group>
    </Stack>
  );
};
//end

View Extra Storybook Examples