Tree data

DataGrid hierarchical tree data with sub-rows

Basic tree data

Pass getSubRows to useDataGrid to define how child rows are read from your data. The tree is rendered in the first visible data column: rows are indented, a chevron toggles rows that have children and guide lines connect the levels:

Name
Type
Size
src
folder—
public
folder—
package.json
file890 B
tsconfig.json
file430 B
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { data } from './data';

function Demo() {
  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'name', header: 'Name' },
      { accessorKey: 'type', header: 'Type' },
      {
        accessorKey: 'size',
        header: 'Size',
        cell: ({ getValue }) => {
          const size = getValue<number | undefined>();
          return size !== undefined ? `${size.toLocaleString()} B` : '—';
        },
      },
    ],
    getSubRows: (row) => row.children,
    getRowId: (row) => row.id,
  });

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

Tree lines

Set withTreeLines={false} to hide the guide lines:

Name
Type
Size
src
folder
components
folder
Button.tsx
file2,400 B
Input.tsx
file1,800 B
Modal.tsx
file3,200 B
hooks
folder
useAuth.ts
file950 B
useTheme.ts
file620 B
index.ts
file150 B
public
folder
favicon.ico
file4,096 B
logo.svg
file1,240 B
package.json
file890 B
tsconfig.json
file430 B
import { FileIcon, FolderIcon, FolderOpenIcon } from '@phosphor-icons/react';
import { Group } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { fileTree } from './data';

function Demo() {
  const store = useDataGrid({
    data: fileTree,
    columns: [
      {
        accessorKey: 'name',
        header: 'Name',
        cell: ({ row, getValue }) => (
          <Group gap={6} wrap="nowrap">
            {row.original.type === 'folder' ? (
              row.getIsExpanded() ? (
                <FolderOpenIcon size={16} />
              ) : (
                <FolderIcon size={16} />
              )
            ) : (
              <FileIcon size={16} />
            )}
            {getValue<string>()}
          </Group>
        ),
      },
      { accessorKey: 'type', header: 'Type' },
      {
        accessorKey: 'size',
        header: 'Size',
        meta: { align: 'end' },
        cell: ({ getValue }) => {
          const size = getValue<number | undefined>();
          return size !== undefined ? `${size.toLocaleString()} B` : '';
        },
      },
    ],
    getSubRows: (row) => row.children,
    getRowId: (row) => row.id,
    tableOptions: { initialState: { expanded: true } },
  });

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

Icons and custom labels

The tree cell renders the output of the column cell renderer, use it to add icons or any other inline content:

Name
Type
Size
src
folder
components
folder
Button.tsx
file2,400 B
Input.tsx
file1,800 B
Modal.tsx
file3,200 B
hooks
folder
useAuth.ts
file950 B
useTheme.ts
file620 B
index.ts
file150 B
public
folder
favicon.ico
file4,096 B
logo.svg
file1,240 B
package.json
file890 B
tsconfig.json
file430 B
import { FileIcon, FolderIcon, FolderOpenIcon } from '@phosphor-icons/react';
import { Group } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { fileTree } from './data';

function Demo() {
  const store = useDataGrid({
    data: fileTree,
    columns: [
      {
        accessorKey: 'name',
        header: 'Name',
        cell: ({ row, getValue }) => (
          <Group gap={6} wrap="nowrap">
            {row.original.type === 'folder' ? (
              row.getIsExpanded() ? (
                <FolderOpenIcon size={16} />
              ) : (
                <FolderIcon size={16} />
              )
            ) : (
              <FileIcon size={16} />
            )}
            {getValue<string>()}
          </Group>
        ),
      },
      { accessorKey: 'type', header: 'Type' },
      {
        accessorKey: 'size',
        header: 'Size',
        meta: { align: 'end' },
        cell: ({ getValue }) => {
          const size = getValue<number | undefined>();
          return size !== undefined ? `${size.toLocaleString()} B` : '';
        },
      },
    ],
    getSubRows: (row) => row.children,
    getRowId: (row) => row.id,
    tableOptions: { initialState: { expanded: true } },
  });

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

Filtering through tree hierarchy

Set filterFromLeafRows to filter from leaf rows upward. When a leaf row matches the filter, its parent rows are kept to preserve the tree structure:

Name
Type
Size
src
folder—
public
folder—
package.json
file890 B
tsconfig.json
file430 B
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { data } from './data';

function Demo() {
  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'name', header: 'Name' },
      { accessorKey: 'type', header: 'Type' },
      {
        accessorKey: 'size',
        header: 'Size',
        cell: ({ getValue }) => {
          const size = getValue<number | undefined>();
          return size !== undefined ? `${size.toLocaleString()} B` : '—';
        },
      },
    ],
    getSubRows: (row) => row.children,
    getRowId: (row) => row.id,
    withGlobalFilter: true,
    filterFromLeafRows: true,
  });

  return (
    <DataGrid store={store}>
      <DataGrid.Toolbar />
    </DataGrid>
  );
}

Pagination with expanded rows

By default, expanded sub-rows count toward the page size. Set paginateExpandedRows: false to count top-level rows only:

Name
Type
Size
src
folder—
public
folder—
package.json
file890 B
tsconfig.json
file430 B
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { data } from './data';

function Demo() {
  const store = useDataGrid({
    data,
    columns: [
      { accessorKey: 'name', header: 'Name' },
      { accessorKey: 'type', header: 'Type' },
      {
        accessorKey: 'size',
        header: 'Size',
        cell: ({ getValue }) => {
          const size = getValue<number | undefined>();
          return size !== undefined ? `${size.toLocaleString()} B` : '—';
        },
      },
    ],
    getSubRows: (row) => row.children,
    getRowId: (row) => row.id,
    pageSize: 4,
    paginateExpandedRows: false,
  });

  return (
    <DataGrid store={store}>
      <DataGrid.Footer />
    </DataGrid>
  );
}

Server-side expansion

Load children in your data fetching logic when expanded changes, then pass the updated nested data to the grid. Use getRowCanExpand to display an expand control before children are loaded. In this example, useFolders is your application's data fetching hook:

import { useState } from 'react';
import { ExpandedState } from '@tanstack/react-table';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { useFolders } from './use-folders';

function Demo() {
  const [expanded, setExpanded] = useState<ExpandedState>({});
  const { data } = useFolders(expanded);

  const store = useDataGrid({
    data,
    columns: [{ accessorKey: 'name', header: 'Name' }],
    getRowId: (row) => row.id,
    getSubRows: (row) => row.children,
    getRowCanExpand: (row) => row.original.hasChildren,
    expanded,
    onExpandedChange: setExpanded,
  });

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

Per-row expand control

Use getRowCanExpand to control which rows can be expanded:

const store = useDataGrid({
  data,
  columns,
  getSubRows: (row) => row.children,
  getRowCanExpand: (row) => (row.original.children?.length ?? 0) > 0,
});

Selection

With withRowSelection, selecting a parent row selects its children. Set enableSubRowSelection: false to select the parent row only:

const store = useDataGrid({
  data,
  columns,
  getRowId: (row) => row.id,
  getSubRows: (row) => row.children,
  withRowSelection: true,
  enableSubRowSelection: false,
});

List view and row reorder

In list view, tree rows are rendered flat without indentation. Row reorder is not available with tree data.