Expanding Sub-Rows (Tree Data) Feature Guide
Material 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 Props
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| Usage Docs | |||
The data for the table to display. This array should match the type you provided to table.setRowType<...>, but in theory could be an array of anything. It's common for each item in the array to be an object of key/values but this is not required. Columns can access this data via string/index or a functional accessor to return anything they want. When the data option changes reference (compared via Object.is), the table will reprocess the data. Any other data processing that relies on the core data model (such as grouping, sorting, filtering, etc) will also be reprocessed. | |||||
2 |
| TanStack Table Expanding Docs | |||
Enable this setting to automatically reset the expanded state of the table when grouping state changes. | |||||
3 |
|
| |||
No Description Provided... Yet... | |||||
4 |
| ||||
No Description Provided... Yet... | |||||
5 |
| ||||
No Description Provided... Yet... | |||||
6 |
| ||||
No Description Provided... Yet... | |||||
7 |
| TanStack Table Expanding Docs | |||
If provided, allows you to override the default behavior of determining whether a row is currently expanded. | |||||
8 |
| TanStack Table Expanding Docs | |||
If provided, allows you to override the default behavior of determining whether a row can be expanded. | |||||
9 |
| TanStack Table Core Table Docs | |||
This optional function is used to access the sub rows for any given row. If you are using nested rows, you will need to use this function to return the sub rows object (or undefined) from the row. | |||||
10 |
| TanStack Table Expanding Docs | |||
Enables manual row expansion. If this is set to true, getExpandedRowModel will not be used to expand rows and you would be expected to perform the expansion in your own data model. This is useful if you are doing server-side expansion. | |||||
11 |
| Material UI IconButton Props | |||
No Description Provided... Yet... | |||||
12 |
| Material UI IconButton Props | |||
No Description Provided... Yet... | |||||
13 |
| TanStack Table Expanding Docs | |||
This function is called when the expanded table state changes. If a function is provided, you will be responsible for managing this state on your own. To pass the managed state back to the table, use the tableOptions.state.expanded option. | |||||
14 |
| TanStack Table Expanding Docs | |||
If true expanded rows will be paginated along with the rest of the table (which means expanded rows may span multiple pages). If false expanded rows will not be considered for pagination (which means expanded rows will always render on their parents page. This also means more rows will be rendered than the set page size) | |||||
15 |
| ||||
No Description Provided... Yet... |
Relevant State Options
# | State Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| TanStack Table Expanding Docs | ||
No Description Provided... Yet... |
Enable Expanding Sub-Rows
To enable expanding sub-rows, you must first set the enableExpanding
prop 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, Material 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 and override this behavior by passing a custom getSubRows
prop.
const data = [{id: 1,name: 'John Doe',subRows: [{id: 2,name: 'Jane Doe',},],},];return (<MaterialReactTablecolumns={columns}data={data}enableExpandinggetSubRows={(originalRow) => originalRow.subRows} //default, can customize/>);
First Name | Last Name | Address | City | State | |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio |
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';34export type Person = {5 firstName: string;6 lastName: string;7 address: string;8 city: string;9 state: string;10 subRows?: Person[]; //Each person can have sub rows of more people11};1213export const data = [14 {15 firstName: 'Dylan',16 lastName: 'Murray',17 address: '261 Erdman Ford',18 city: 'East Daphne',19 state: 'Kentucky',20 subRows: [21 {22 firstName: 'Ervin',23 lastName: 'Reinger',24 address: '566 Brakus Inlet',25 city: 'South Linda',26 state: 'West Virginia',27 subRows: [28 {29 firstName: 'Jordane',30 lastName: 'Homenick',31 address: '1234 Brakus Inlet',32 city: 'South Linda',33 state: 'West Virginia',34 },35 ],36 },37 {38 firstName: 'Brittany',39 lastName: 'McCullough',40 address: '722 Emie Stream',41 city: 'Lincoln',42 state: 'Nebraska',43 },44 ],45 },46 {47 firstName: 'Raquel',48 lastName: 'Kohler',49 address: '769 Dominic Grove',50 city: 'Columbus',51 state: 'Ohio',52 subRows: [53 {54 firstName: 'Branson',55 lastName: 'Frami',56 address: '32188 Larkin Turnpike',57 city: 'Charleston',58 state: 'South Carolina',59 },60 ],61 },62];6364const Example: FC = () => {65 const columns = useMemo<MRT_ColumnDef<Person>[]>(66 //column definitions...94 );9596 return (97 <MaterialReactTable98 columns={columns}99 data={data}100 enableExpanding101 enableExpandAll //default102 />103 );104};105106export default Example;107
Expand All Rows Button
By default, Material React Table will show the expand all button in the expand column header. You can disable this by setting the enableExpandAll
prop to false
.
<MaterialReactTablecolumns={columns}data={data}enableExpandingenableExpandAll={false} //hide expand all button in header/>
Expanded Rows Pagination Behavior
By default, Material 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
prop to false
.
<MaterialReactTablecolumns={columns}data={data}enableExpandingpaginateExpandedRows={false} //expanded rows will be on the same page as their parent row/>
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
props.
For example, you may want all rows to be expanded by default. You can simply set the expanded
state option to true
to do this.
<MaterialReactTablecolumns={columns}data={data}enableExpandinginitialState={{ expanded: true }} //all rows expanded by default/>
Expand | First Name | Last Name | Address | City | State |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | |
Jordane | Homenick | 1234 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';34export type Person = {5 firstName: string;6 lastName: string;7 address: string;8 city: string;9 state: string;10 subRows?: Person[]; //Each person can have sub rows of more people11};1213export const data = [14 {15 firstName: 'Dylan',16 lastName: 'Murray',17 address: '261 Erdman Ford',18 city: 'East Daphne',19 state: 'Kentucky',20 subRows: [21 {22 firstName: 'Ervin',23 lastName: 'Reinger',24 address: '566 Brakus Inlet',25 city: 'South Linda',26 state: 'West Virginia',27 subRows: [28 {29 firstName: 'Jordane',30 lastName: 'Homenick',31 address: '1234 Brakus Inlet',32 city: 'South Linda',33 state: 'West Virginia',34 },35 ],36 },37 {38 firstName: 'Brittany',39 lastName: 'McCullough',40 address: '722 Emie Stream',41 city: 'Lincoln',42 state: 'Nebraska',43 },44 ],45 },46 {47 firstName: 'Raquel',48 lastName: 'Kohler',49 address: '769 Dominic Grove',50 city: 'Columbus',51 state: 'Ohio',52 subRows: [53 {54 firstName: 'Branson',55 lastName: 'Frami',56 address: '32188 Larkin Turnpike',57 city: 'Charleston',58 state: 'South Carolina',59 },60 ],61 },62];6364const Example: FC = () => {65 const columns = useMemo<MRT_ColumnDef<Person>[]>(66 //column definitions...94 );9596 return (97 <MaterialReactTable98 columns={columns}99 data={data}100 enableExpanding101 enableExpandAll={false}102 initialState={{ expanded: true }} //expand all rows by default103 paginateExpandedRows={false}104 />105 );106};107108export default Example;109