MRT logoMantine React Table

On This Page

    Expanding Sub-Rows (Tree Data) Feature Guide

    Mantine React Table has support for expanding sub-rows or tree data. This feature is useful for displaying hierarchical data. The sub-rows can be expanded and collapsed by clicking on the expand/collapse icon.

    NOTE: This feature is for expanding rows of the same data type. If you want to add expansion of more data for the same row, check out the Detail Panel Feature Guide.

    Relevant Table Options

    #
    Prop Name
    Type
    Default Value
    More Info Links
    1booleanTanStack Table Expanding Docs
    2Array<TData>Usage Docs
    3booleantrueMRT Expanding Sub Rows Docs
    4booleanMRT Expanding Sub Rows Docs
    5(dataRow: TData) => TData[]
    6booleanfalseTanStack Filtering Docs
    7() => MRT_RowModel<TData>
    8(row: Row<TData>) => booleanTanStack Table Expanding Docs
    9(row: Row<TData>) => booleanTanStack Table Expanding Docs
    10(originalRow: TData, index: number) => undefined | TData[]TanStack Table Core Table Docs
    11ActionIconProps | ({ table }) => ActionIconPropsMantine ActionIcon Docs
    12ActionIconProps | ({ row, table }) => ActionIconPropsMantine ActionIcon Docs
    13booleanTanStack Table Expanding Docs
    14number100TanStack Table Filtering Docs
    15OnChangeFn<ExpandedState>TanStack Table Expanding Docs
    16booleanTanStack Table Expanding Docs
    17'first' | 'last'

    Relevant State Options

    #
    State Option
    Type
    Default Value
    More Info Links
    1Record<string, boolean> | boolean{}TanStack Table Expanding Docs

    Enable Expanding Sub-Rows

    To enable expanding sub-rows, you must first set the enableExpanding table option to true.

    However, your data must also be formatted in a way to allow for expanding rows that are in some way related to each other. By default, Mantine React Table will look for a special subRows property on each row of your data and treat any array of rows that it finds as the sub-rows for that row. You can customize or override this behavior by passing a custom getSubRows table option.

    const data = [
      {
        id: 1,
        name: 'John Doe',
        subRows: [
          {
            id: 2,
            name: 'Jane Doe',
          },
        ],
      },
    ];
    
    const table = useMantineReactTable({
      columns,
      data,
      enableExpanding: true,
      getSubRows: (originalRow) => originalRow.subRows, //default, can customize
    });
    
    return <MantineReactTable table={table} />;
    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    RaquelKohler769 Dominic GroveColumbusOhio

    Rows per page

    1-2 of 2

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo } from 'react';
    import { MantineReactTable, type MRT_ColumnDef } from 'mantine-react-table';
    
    export type Person = {
      firstName: string;
      lastName: string;
      address: string;
      city: string;
      state: string;
      subRows?: Person[]; //Each person can have sub rows of more people
    };
    
    export const data = [
      {
        firstName: 'Dylan',
        lastName: 'Murray',
        address: '261 Erdman Ford',
        city: 'East Daphne',
        state: 'Kentucky',
        subRows: [
          {
            firstName: 'Ervin',
            lastName: 'Reinger',
            address: '566 Brakus Inlet',
            city: 'South Linda',
            state: 'West Virginia',
            subRows: [
              {
                firstName: 'Jordane',
                lastName: 'Homenick',
                address: '1234 Brakus Inlet',
                city: 'South Linda',
                state: 'West Virginia',
              },
            ],
          },
          {
            firstName: 'Brittany',
            lastName: 'McCullough',
            address: '722 Emie Stream',
            city: 'Lincoln',
            state: 'Nebraska',
          },
        ],
      },
      {
        firstName: 'Raquel',
        lastName: 'Kohler',
        address: '769 Dominic Grove',
        city: 'Columbus',
        state: 'Ohio',
        subRows: [
          {
            firstName: 'Branson',
            lastName: 'Frami',
            address: '32188 Larkin Turnpike',
            city: 'Charleston',
            state: 'South Carolina',
          },
        ],
      },
    ];
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
    
          {
            accessorKey: 'state',
            enableColumnOrdering: false,
            header: 'State',
          },
        ],
        [],
      );
    
      return (
        <MantineReactTable
          columns={columns}
          data={data}
          enableExpanding
          enableExpandAll //default
        />
      );
    };
    
    export default Example;

    Expand All Rows Button

    By default, Mantine React Table will show the expand all button in the expand column header. You can disable this by setting the enableExpandAll table option to false.

    const table = useMantineReactTable({
      columns,
      data,
      enableExpanding: true,
      enableExpandAll: false, //hide expand all button in header
    });

    Expanded Rows Pagination Behavior

    By default, Mantine React Table will treat expanded sub-rows the same as any other row when it comes to pagination. This means that some expanded rows may be on the next page. You can change this behavior by setting the paginateExpandedRows table option to false.

    const table = useMantineReactTable({
      columns,
      data,
      enableExpanding: true,
      paginateExpandedRows: false, //expanded rows will be on the same page as their parent row
    });

    Expanded Leaf Row Filtering Behavior

    If you are using the filtering features alongside sub-row features, then there are a few behaviors and customizations you should be aware of.

    Filter From Leaf Rows

    By default, filtering is done from parent rows down (so if a parent row is filtered out, all of its children will be filtered out as well). Setting the filterFromLeafRows table option to true will cause filtering to be done from leaf rows up (which means parent rows will be kept so long as one of their child, or grand-child, etc. rows pass the filtering).

    const table = useMantineReactTable({
      columns,
      data,
      enableExpanding: true,
      filterFromLeafRows: true, //search for child rows and preserve parent rows
    });

    Max Leaf Row Filter Depth

    By default, filtering is done for all rows (max depth of 100), no matter if they are root level parent rows or the child leaf rows of a parent row. Setting the maxLeafRowFilterDepth table option to 0 will cause filtering to only be applied to the root level parent rows, with all sub-rows remaining unfiltered. Similarly, setting this option to 1 will cause filtering to only be applied to child leaf rows 1 level deep, and so on.

    This is useful for situations where you want a row's entire child hierarchy to be visible, regardless of the applied filter.

    const table = useMantineReactTable({
      columns,
      data,
      enableExpanding: true,
      maxLeafRowFilterDepth: 0, //When filtering root rows, keep all child rows of the passing parent rows
    });

    Expand All Rows By Default

    You can manage the initial state of the expanded rows with the expanded state option in either the initialState or state table options.

    For example, you may want all rows to be expanded by default. To do this, you can simply set the expanded state option to true.

    const table = useMantineReactTable({
      columns,
      data,
      enableExpanding: true,
      initialState: { expanded: true }, //all rows expanded by default
    });
    Expand
    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    ErvinReinger566 Brakus InletSouth LindaWest Virginia
    JordaneHomenick1234 Brakus InletSouth LindaWest Virginia
    JordanClarkson4882 Palm RdSan FranciscoCalifornia
    BrittanyMcCullough722 Emie StreamLincolnNebraska
    RaquelKohler769 Dominic GroveColumbusOhio
    BransonFrami32188 Larkin TurnpikeCharlestonSouth Carolina

    Rows per page

    1-2 of 2

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo } from 'react';
    import { MantineReactTable, type MRT_ColumnDef } from 'mantine-react-table';
    
    export type Person = {
      firstName: string;
      lastName: string;
      address: string;
      city: string;
      state: string;
      subRows?: Person[]; //Each person can have sub rows of more people
    };
    
    export const data: Person[] = [
      {
        firstName: 'Dylan',
        lastName: 'Murray',
        address: '261 Erdman Ford',
        city: 'East Daphne',
        state: 'Kentucky',
        subRows: [
          {
            firstName: 'Ervin',
            lastName: 'Reinger',
            address: '566 Brakus Inlet',
            city: 'South Linda',
            state: 'West Virginia',
            subRows: [
              {
                firstName: 'Jordane',
                lastName: 'Homenick',
                address: '1234 Brakus Inlet',
                city: 'South Linda',
                state: 'West Virginia',
              },
              {
                firstName: 'Jordan',
                lastName: 'Clarkson',
                address: '4882 Palm Rd',
                city: 'San Francisco',
                state: 'California',
              },
            ],
          },
          {
            firstName: 'Brittany',
            lastName: 'McCullough',
            address: '722 Emie Stream',
            city: 'Lincoln',
            state: 'Nebraska',
          },
        ],
      },
      {
        firstName: 'Raquel',
        lastName: 'Kohler',
        address: '769 Dominic Grove',
        city: 'Columbus',
        state: 'Ohio',
        subRows: [
          {
            firstName: 'Branson',
            lastName: 'Frami',
            address: '32188 Larkin Turnpike',
            city: 'Charleston',
            state: 'South Carolina',
          },
        ],
      },
    ];
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
    
          {
            accessorKey: 'state',
            enableColumnOrdering: false,
            header: 'State',
          },
        ],
        [],
      );
    
      return (
        <MantineReactTable
          columns={columns}
          data={data}
          enableExpandAll={false} //hide expand all double arrow in column header
          enableExpanding
          filterFromLeafRows //apply filtering to all rows instead of just parent rows
          initialState={{ expanded: true }} //expand all rows by default
          paginateExpandedRows={false} //When rows are expanded, do not count sub-rows as number of rows on the page towards pagination
        />
      );
    };
    
    export default Example;

    Expand Root Rows Only By Default

    Here is a slightly more complex initial expanded state example where all the root rows are expanded by default, but none of the sub rows themselves are expanded by default. We just need to find all of the root row ids and set their key in the expanded initialState option to true.

    First Name
    Last Name
    Address
    City
    State
    DylanMurray261 Erdman FordEast DaphneKentucky
    ErvinReinger566 Brakus InletSouth LindaWest Virginia
    BrittanyMcCullough722 Emie StreamLincolnNebraska
    RaquelKohler769 Dominic GroveColumbusOhio
    BransonFrami32188 Larkin TurnpikeCharlestonSouth Carolina

    Rows per page

    1-5 of 5

    import '@mantine/core/styles.css';
    import '@mantine/dates/styles.css'; //if using mantine date picker features
    import 'mantine-react-table/styles.css'; //make sure MRT styles were imported in your app root (once)
    import { useMemo } from 'react';
    import {
      MantineReactTable,
      type MRT_ExpandedState,
      type MRT_ColumnDef,
    } from 'mantine-react-table';
    import { Button } from '@mantine/core';
    
    export type Person = {
      id: string;
      firstName: string;
      lastName: string;
      address: string;
      city: string;
      state: string;
      subRows?: Person[]; //Each person can have sub rows of more people
    };
    
    export const data: Person[] = [
      {
        id: '1',
        firstName: 'Dylan',
        lastName: 'Murray',
        address: '261 Erdman Ford',
        city: 'East Daphne',
        state: 'Kentucky',
        subRows: [
          {
            id: '2',
            firstName: 'Ervin',
            lastName: 'Reinger',
            address: '566 Brakus Inlet',
            city: 'South Linda',
            state: 'West Virginia',
            subRows: [
              {
                id: '3',
                firstName: 'Jordane',
                lastName: 'Homenick',
                address: '1234 Brakus Inlet',
                city: 'South Linda',
                state: 'West Virginia',
              },
              {
                id: '4',
                firstName: 'Jordan',
                lastName: 'Clarkson',
                address: '4882 Palm Rd',
                city: 'San Francisco',
                state: 'California',
              },
            ],
          },
          {
            id: '5',
            firstName: 'Brittany',
            lastName: 'McCullough',
            address: '722 Emie Stream',
            city: 'Lincoln',
            state: 'Nebraska',
          },
        ],
      },
      {
        id: '6',
        firstName: 'Raquel',
        lastName: 'Kohler',
        address: '769 Dominic Grove',
        city: 'Columbus',
        state: 'Ohio',
        subRows: [
          {
            id: '7',
            firstName: 'Branson',
            lastName: 'Frami',
            address: '32188 Larkin Turnpike',
            city: 'Charleston',
            state: 'South Carolina',
            subRows: [
              {
                id: '8',
                firstName: 'Henry',
                lastName: 'Ford',
                address: '1234 Brakus Inlet',
                city: 'Nashville',
                state: 'Tennessee',
              },
            ],
          },
        ],
      },
    ];
    
    const Example = () => {
      const columns = useMemo<MRT_ColumnDef<Person>[]>(
        () => [
          {
            accessorKey: 'firstName',
            header: 'First Name',
          },
          {
            accessorKey: 'lastName',
            header: 'Last Name',
          },
    
          {
            accessorKey: 'address',
            header: 'Address',
          },
          {
            accessorKey: 'city',
            header: 'City',
          },
    
          {
            accessorKey: 'state',
            enableColumnOrdering: false,
            header: 'State',
          },
        ],
        [],
      );
    
      const initialExpandedRootRows = useMemo<MRT_ExpandedState>(
        () =>
          data
            .map((originalRow) => originalRow.id) //get all the root row ids, use recursion for additional levels
            .reduce((a, v) => ({ ...a, [v]: true }), {}), //convert to an object with all the ids as keys and `true` as values
        [],
      );
    
      return (
        <MantineReactTable
          columns={columns}
          data={data}
          enableExpanding
          getRowId={(originalRow) => originalRow.id}
          initialState={{ expanded: initialExpandedRootRows }} //only expand the root rows by default
          renderTopToolbarCustomActions={({ table }) => (
            <Button onClick={() => table.resetExpanded()}>Reset Expanded</Button>
          )}
        />
      );
    };
    
    export default Example;