Examples › Column grouping

Sometimes a subset of columns is closely related, in which case it makes sense to group their headers under a shared title. You can do this by passing the groups prop to the DataTable instead of columns.

Each group requires the following properties:

  • id
    Used as a key. It can be any string, as long as it is unique among the groups.
    A humanized version of this value is used as the group header if no title is provided.
  • columns
    An array of column definitions that are part of this group.
    Alternatively, you can provide nested groups instead of columns — see multilevel column grouping below.

A group also accepts a few optional properties for customization purposes:

  • title
    A ReactNode to render inside the group header. If not specified, the id is humanized to generate a title.
  • textAlign: 'left' | 'center' | 'right'
    The text alignment of the group header. It doesn’t affect the alignment of the columns in the group, which you can set individually.
  • className: string
    Optional class name to apply to the group header.
  • style
    Optional style to apply to the group header.
    Can be a style object or a function that receives the current theme and returns a style object.

Groups are hidden if they don’t have any visible columns. This can happen when all of their columns are hidden by the hidden or visibleMediaQuery properties, or when the group has an empty array of columns.

CompanyContact information
Name
Mission statement
Street address
City
State
Feest, Bogan and HerzogInnovate bricks-and-clicks metrics.21716 Ratke DriveStromanportWY
Cummerata - KuhlmanHarness real-time channels.6389 Dicki StreamSouth GateNH
Goyette IncProductize front-end web services.8873 Mertz RapidDorthysideID
Runte IncEngage synergistic infrastructures.2996 Ronny MountMcAllenMA
Goldner, Rohan and LehnerIncubate cross-platform metrics.632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'ConnellScale web-enabled e-business.576 Joyce WaysTyraburghKS
Rau - O'HaraInnovate real-time applications.7508 Lansdowne RoadShieldsboroughMI
Tillman - JacobiMatrix viral synergies.57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and HodkiewiczMaximize dynamic e-commerce.7057 Stanley RoadKearaburghCA
Shanahan, Robel and BeierSynthesize customized portals.378 Berta CrescentWest GerryKS
Kling - McLaughlinReinvent cross-platform channels.8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlinEliminate best-of-breed e-markets.83462 Shazam StreetNorth JoesphID
Jogi - McLaughlinEliminate best-of-breed e-markets.83462 Shazam StreetNorth JoesphID
No records

Here is the code used to generate the table above:

'use client';

import { DataTable } from 'mantine-datatable';
import { companies } from '~/data';

export function ColumnGroupingExample() {
  return (
    <DataTable
      withTableBorder
      withColumnBorders
      records={companies}
      groups={[
        {
          id: 'company',
          style: { fontStyle: 'italic' },
          columns: [
            { accessor: 'name' },
            { accessor: 'missionStatement', visibleMediaQuery: (theme) => `(min-width: ${theme.breakpoints.md})` },
          ],
        },
        {
          id: 'contact-info',
          title: 'Contact information',
          textAlign: 'center',
          style: (theme) => ({ color: theme.colors.blue[6] }),
          columns: [{ accessor: 'streetAddress' }, { accessor: 'city' }, { accessor: 'state', textAlign: 'right' }],
        },
        // 👇 all columns in this group are hidden, so it will not be rendered
        {
          id: 'other',
          columns: [{ accessor: 'id', hidden: true }],
        },
        // 👇 this group has no columns, so it will not be rendered
        {
          id: 'empty-group',
          title: 'Empty group',
          columns: [],
        },
      ]}
    />
  );
}

Multilevel column grouping

Groups can be nested to create multiple header rows. To do this, provide a groups array instead of columns for a group; each nested group can, in turn, contain either columns or further groups, but not both.

Branches don’t need to have the same depth: if a group containing columns sits at a shallower level than others, its header will span the extra header rows. The order of the leaf columns in the table follows the order in which they appear in the group tree.

Personal InformationContact InformationWork Information
NameDemographicsIDs
First Name
Last Name
Gender
Birth Date
Email Address
Employee ID
Department ID
JeraldHowellmale5/21/1950Jerald.Howell32@yahoo.com69f81cf4-aac7-45f4-8567-0c03405edc5272bc79a2-eb57-473e-bd35-8c72ce68f65d
KathleenRueckerfemale12/19/1943Kathleen_Ruecker@hotmail.com7cd87585-5e02-45f6-881a-80cbb2c83cea9a9af102-cf80-4393-a3a5-f54c1ad0ad24
EricaVolkmanfemale1/29/1955Erica.Volkman37@gmail.com060b7b1b-deba-4e38-9b75-82e69f1bae06d7cca8f2-cb18-4e2c-a207-88257f540341
CliffordOberbrunnermale3/6/1979Clifford.Oberbrunner@hotmail.com84e46e16-efb9-4640-87a2-5108cb386cf2cee34a9b-e68b-49f8-a368-66f097f5c6fe
AlisonKlingfemale1/27/1997Alison16@gmail.comf14ab942-f6f2-413e-9495-a3a1c1fa0f8c049d164f-1649-4737-8c42-37348588d3bc
SueZiemefemale9/12/1960Sue.Zieme29@hotmail.combca16bee-ff3e-4b58-8c60-e39e4199ecafb7841b69-7207-41ed-9327-270c674619eb
FeliciaGleasonfemale3/9/1977Felicia30@yahoo.comd7e6a066-12bd-4f26-876b-4dc2eed3eabf2c664bed-50e2-439a-b4fd-af874f3eba53
AlfredoZemlakmale11/12/1988Alfredo22@yahoo.come0624354-4d3c-48b0-b579-492954da168e4d1e93ad-5189-469b-8aec-d92e794c20be
EmilyBartolettifemale1/5/1968Emily.Bartoletti@gmail.come57eaa3f-a968-49dc-9db4-cbfd463aa0e5cee34a9b-e68b-49f8-a368-66f097f5c6fe
DeloresReynoldsfemale6/4/2004Delores.Reynolds@yahoo.comc27a6249-5c0b-40cc-a519-fa4ed9ac4b9672bc79a2-eb57-473e-bd35-8c72ce68f65d
No records

Here is the code used to generate the table above:

'use client';

import { DataTable } from 'mantine-datatable';
import { employees } from '~/data';

export function MultilevelColumnGroupingExample() {
  return (
    <DataTable
      withTableBorder
      withColumnBorders
      records={employees.slice(0, 10)}
      groups={[
        {
          id: 'personal-info',
          title: 'Personal Information',
          groups: [
            {
              id: 'name-group',
              title: 'Name',
              columns: [
                {
                  accessor: 'firstName',
                  title: 'First Name',
                  width: 120,
                },
                {
                  accessor: 'lastName',
                  title: 'Last Name',
                  width: 120,
                },
              ],
            },
            {
              id: 'demographics',
              title: 'Demographics',
              columns: [
                {
                  accessor: 'sex',
                  title: 'Gender',
                  width: 80,
                },
                {
                  accessor: 'birthDate',
                  title: 'Birth Date',
                  width: 120,
                  render: ({ birthDate }) => new Date(birthDate).toLocaleDateString(),
                },
              ],
            },
          ],
        },
        {
          // 👇 a group with columns can sit next to groups with nested groups;
          //    its header will span the extra header rows
          id: 'contact-info',
          title: 'Contact Information',
          columns: [
            {
              accessor: 'email',
              title: 'Email Address',
              width: 250,
              ellipsis: true,
            },
          ],
        },
        {
          id: 'work-info',
          title: 'Work Information',
          groups: [
            {
              id: 'identifiers',
              title: 'IDs',
              columns: [
                {
                  accessor: 'id',
                  title: 'Employee ID',
                  width: 100,
                  ellipsis: true,
                },
                {
                  accessor: 'department.id',
                  title: 'Department ID',
                  width: 100,
                  ellipsis: true,
                },
              ],
            },
          ],
        },
      ]}
    />
  );
}

Head over to the next example to discover more features.

Mantine DataTable is used and trusted by