Examples › Nested tables

There is no dedicated “tree table” component, but you can easily build nested tables by combining the row expansion feature with the noHeader property: each expanded row renders a header-less DataTable whose columns line up with the ones of its parent.

Click the expandable rows in the table below to see it in action:

Company / Department / Employee
Employees / Birth date
Feest, Bogan and Herzog37
Cummerata - Kuhlman67
Goyette Inc63
Runte Inc87
Goldner, Rohan and Lehner29
Doyle, Hodkiewicz and O'Connell61
Rau - O'Hara41
Tillman - Jacobi46
Connelly, Feest and Hodkiewicz38
Shanahan, Robel and Beier31
Kling - McLaughlin0
Jogi - McLaughlin0
Jogi - McLaughlin0
No records

A few things worth noting about the implementation:

  • Each level keeps its expanded record IDs in state and passes them to rowExpansion.expanded (recordIds and onRecordIdsChange). Controlling the expansion state this way lets the column render functions know which rows are expanded, so they can rotate the chevron icons accordingly.
  • allowMultiple: true lets the user expand several companies or departments at the same time.
  • Nested tables use the same fixed width for the last column as their parent, so the columns stay aligned, while a left margin on the first cell content gives each level a visual indentation.
  • If some of your records have no children, use the rowExpansion.expandable function to prevent users from expanding them.

Here is the code:

'use client';

import { DataTable } from 'mantine-datatable';
import { Box } from '@mantine/core';
import { IconBuilding, IconChevronRight, IconUser, IconUsers } from '@tabler/icons-react';
import clsx from 'clsx';
import dayjs from 'dayjs';
import { useState } from 'react';
import { employees } from '~/data';
import { companies, departments } from '~/data/nested';
import classes from './NestedTablesExample.module.css';

export function NestedTablesExample() {
  const [expandedCompanyIds, setExpandedCompanyIds] = useState<string[]>([]);
  const [expandedDepartmentIds, setExpandedDepartmentIds] = useState<string[]>([]);

  return (
    <DataTable
      withTableBorder
      withColumnBorders
      highlightOnHover
      columns={[
        {
          accessor: 'name',
          title: 'Company / Department / Employee',
          noWrap: true,
          render: ({ id, name }) => (
            <>
              <IconChevronRight
                className={clsx(classes.icon, classes.expandIcon, {
                  [classes.expandIconRotated]: expandedCompanyIds.includes(id),
                })}
              />
              <IconBuilding className={classes.icon} />
              <span>{name}</span>
            </>
          ),
        },
        { accessor: 'employees', title: 'Employees / Birth date', textAlign: 'right', width: 200 },
      ]}
      records={companies}
      rowExpansion={{
        allowMultiple: true,
        expanded: { recordIds: expandedCompanyIds, onRecordIdsChange: setExpandedCompanyIds },
        content: (company) => (
          <DataTable
            noHeader
            withColumnBorders
            minHeight={100}
            columns={[
              {
                accessor: 'name',
                noWrap: true,
                render: ({ id, name }) => (
                  <Box component="span" ml={20}>
                    <IconChevronRight
                      className={clsx(classes.icon, classes.expandIcon, {
                        [classes.expandIconRotated]: expandedDepartmentIds.includes(id),
                      })}
                    />
                    <IconUsers className={classes.icon} />
                    <span>{name}</span>
                  </Box>
                ),
              },
              { accessor: 'employees', textAlign: 'right', width: 200 },
            ]}
            records={departments.filter((department) => department.company.id === company.record.id)}
            rowExpansion={{
              allowMultiple: true,
              expanded: { recordIds: expandedDepartmentIds, onRecordIdsChange: setExpandedDepartmentIds },
              content: (department) => (
                <DataTable
                  noHeader
                  withColumnBorders
                  columns={[
                    {
                      accessor: 'name',
                      render: ({ firstName, lastName }) => (
                        <Box component="span" ml={40}>
                          <IconUser className={classes.icon} />
                          <span>
                            {firstName} {lastName}
                          </span>
                        </Box>
                      ),
                    },
                    {
                      accessor: 'birthDate',
                      render: ({ birthDate }) => dayjs(birthDate).format('DD MMM YYYY'),
                      textAlign: 'right',
                      width: 200,
                    },
                  ]}
                  records={employees.filter((employee) => employee.department.id === department.record.id)}
                />
              ),
            }}
          />
        ),
      }}
    />
  );
}

Head over to the next example to see how you can load nested table data asynchronously.

Mantine DataTable is used and trusted by