# Tree data
Package: @mantine/data-grid
Import: import { DataGridTreeData } from '@mantine/data-grid';
Description: 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:

```tsx
// Demo.tsx
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} />;
}

// data.ts
interface FileItem {
  id: string;
  name: string;
  type: 'folder' | 'file';
  size?: number;
  children?: FileItem[];
}

export const data: FileItem[] = [
  {
    id: '1',
    name: 'src',
    type: 'folder',
    children: [
      {
        id: '1-1',
        name: 'components',
        type: 'folder',
        children: [
          { id: '1-1-1', name: 'Button.tsx', type: 'file', size: 2400 },
          { id: '1-1-2', name: 'Input.tsx', type: 'file', size: 1800 },
          { id: '1-1-3', name: 'Modal.tsx', type: 'file', size: 3200 },
        ],
      },
      {
        id: '1-2',
        name: 'hooks',
        type: 'folder',
        children: [
          { id: '1-2-1', name: 'useAuth.ts', type: 'file', size: 950 },
          { id: '1-2-2', name: 'useTheme.ts', type: 'file', size: 620 },
        ],
      },
      { id: '1-3', name: 'index.ts', type: 'file', size: 150 },
    ],
  },
  {
    id: '2',
    name: 'public',
    type: 'folder',
    children: [
      { id: '2-1', name: 'favicon.ico', type: 'file', size: 4096 },
      { id: '2-2', name: 'logo.svg', type: 'file', size: 1240 },
    ],
  },
  { id: '3', name: 'package.json', type: 'file', size: 890 },
  { id: '4', name: 'tsconfig.json', type: 'file', size: 430 },
];
```


## Tree lines

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

```tsx
// Demo.tsx
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:

```tsx
// Demo.tsx
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:

```tsx
// Demo.tsx
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>
  );
}

// data.ts
interface FileItem {
  id: string;
  name: string;
  type: 'folder' | 'file';
  size?: number;
  children?: FileItem[];
}

export const data: FileItem[] = [
  {
    id: '1',
    name: 'src',
    type: 'folder',
    children: [
      {
        id: '1-1',
        name: 'components',
        type: 'folder',
        children: [
          { id: '1-1-1', name: 'Button.tsx', type: 'file', size: 2400 },
          { id: '1-1-2', name: 'Input.tsx', type: 'file', size: 1800 },
          { id: '1-1-3', name: 'Modal.tsx', type: 'file', size: 3200 },
        ],
      },
      {
        id: '1-2',
        name: 'hooks',
        type: 'folder',
        children: [
          { id: '1-2-1', name: 'useAuth.ts', type: 'file', size: 950 },
          { id: '1-2-2', name: 'useTheme.ts', type: 'file', size: 620 },
        ],
      },
      { id: '1-3', name: 'index.ts', type: 'file', size: 150 },
    ],
  },
  {
    id: '2',
    name: 'public',
    type: 'folder',
    children: [
      { id: '2-1', name: 'favicon.ico', type: 'file', size: 4096 },
      { id: '2-2', name: 'logo.svg', type: 'file', size: 1240 },
    ],
  },
  { id: '3', name: 'package.json', type: 'file', size: 890 },
  { id: '4', name: 'tsconfig.json', type: 'file', size: 430 },
];
```


## Pagination with expanded rows

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

```tsx
// Demo.tsx
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>
  );
}

// data.ts
interface FileItem {
  id: string;
  name: string;
  type: 'folder' | 'file';
  size?: number;
  children?: FileItem[];
}

export const data: FileItem[] = [
  {
    id: '1',
    name: 'src',
    type: 'folder',
    children: [
      {
        id: '1-1',
        name: 'components',
        type: 'folder',
        children: [
          { id: '1-1-1', name: 'Button.tsx', type: 'file', size: 2400 },
          { id: '1-1-2', name: 'Input.tsx', type: 'file', size: 1800 },
          { id: '1-1-3', name: 'Modal.tsx', type: 'file', size: 3200 },
        ],
      },
      {
        id: '1-2',
        name: 'hooks',
        type: 'folder',
        children: [
          { id: '1-2-1', name: 'useAuth.ts', type: 'file', size: 950 },
          { id: '1-2-2', name: 'useTheme.ts', type: 'file', size: 620 },
        ],
      },
      { id: '1-3', name: 'index.ts', type: 'file', size: 150 },
    ],
  },
  {
    id: '2',
    name: 'public',
    type: 'folder',
    children: [
      { id: '2-1', name: 'favicon.ico', type: 'file', size: 4096 },
      { id: '2-2', name: 'logo.svg', type: 'file', size: 1240 },
    ],
  },
  { id: '3', name: 'package.json', type: 'file', size: 890 },
  { id: '4', name: 'tsconfig.json', type: 'file', size: 430 },
  {
    id: '5',
    name: 'tests',
    type: 'folder',
    children: [
      { id: '5-1', name: 'Button.test.tsx', type: 'file', size: 1200 },
      { id: '5-2', name: 'Input.test.tsx', type: 'file', size: 980 },
    ],
  },
  { id: '6', name: '.gitignore', type: 'file', size: 120 },
  { id: '7', name: 'README.md', type: 'file', size: 2800 },
];
```


## 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:

```tsx
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:

```tsx
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:

```tsx
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.
