Detail Panel (Expanding) Feature Guide
Material React Table has multiple kinds of expanding features. This guide will show you how to use the detail panel feature to expand a single row to show more information for that row.
If you are looking for how to expand multiple rows from a tree data structure, see the Expanding Sub-Rows guide.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| MRT Display Columns Docs | |||
2 |
| ||||
3 |
| ||||
Relevant State
# | State Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| TanStack Table Expanding Docs | ||
Render Detail Panel
To add a detail panel to a row, all you need to do is add a renderDetailPanel
table option.
The recommended way to access the row data for the detail panel is to pull from the original
object on a row. This gives you the original data for the row, not transformed or filtered by TanStack Table.
Using
row.getValue('columnId')
will not work for data that does not have its own column. Usingrow.original.columnId
is recommended for detail panels since the data in the detail panel usually does not have its own column.
ID | First Name | Middle Name | Last Name | |
---|---|---|---|---|
1 | Dylan | Sprouse | Murray | |
2 | Raquel | Hakeem | Kohler | |
3 | Ervin | Kris | Reinger | |
4 | Brittany | Kathryn | McCullough | |
5 | Branson | John | Frami | |
1-5 of 5
1import { useMemo } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6} from 'material-react-table';7import { Box, Typography } from '@mui/material';8import { data, type Person } from './makeData';910const Example = () => {11 const columns = useMemo(12 () =>13 [14 {15 accessorKey: 'id',16 header: 'ID',17 size: 50,18 },19 {20 accessorKey: 'firstName',21 header: 'First Name',22 },23 {24 accessorKey: 'middleName',25 header: 'Middle Name',26 },27 {28 accessorKey: 'lastName',29 header: 'Last Name',30 },31 ] as MRT_ColumnDef<Person>[],32 [],33 );3435 const table = useMaterialReactTable({36 columns,37 data,38 renderDetailPanel: ({ row }) => (39 <Box40 sx={{41 display: 'grid',42 margin: 'auto',43 gridTemplateColumns: '1fr 1fr',44 width: '100%',45 }}46 >47 <Typography>Address: {row.original.address}</Typography>48 <Typography>City: {row.original.city}</Typography>49 <Typography>State: {row.original.state}</Typography>50 <Typography>Country: {row.original.country}</Typography>51 </Box>52 ),53 });5455 return <MaterialReactTable table={table} />;56};5758export default Example;59
Expand Detail Panel By Default
If you want some or all rows to be expanded by default, you can specify that in the initialState.expanded
table option. Pass true
to expand all rows, or specify which rowIds should be expanded.
const table = useMaterialReactTable({data,columns,initialState: {expanded: true,// or expand specific rows by default// expanded: {// 1: true,// 2: true,// },},});
Position Expand Column Last
If you want to position the expand column last, you can set the positionExpandColumn
table option to 'last'
.
ID | First Name | Middle Name | Last Name | |
---|---|---|---|---|
1 | Dylan | Sprouse | Murray | |
Address: 261 Erdman Ford City: East Daphne State: Kentucky Country: United States | ||||
2 | Raquel | Hakeem | Kohler | |
Address: 769 Dominic Grove City: Vancouver State: British Columbia Country: Canada | ||||
3 | Ervin | Kris | Reinger | |
Address: 566 Brakus Inlet City: South Linda State: West Virginia Country: United States |
1-3 of 3
1import { useMemo } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6} from 'material-react-table';7import { Box, Typography } from '@mui/material';8import { data, type Person } from './makeData';910const Example = () => {11 const columns = useMemo<MRT_ColumnDef<Person>[]>(12 () => [13 {14 accessorKey: 'id',15 header: 'ID',16 size: 50,17 },18 {19 accessorKey: 'firstName',20 header: 'First Name',21 },22 {23 accessorKey: 'middleName',24 header: 'Middle Name',25 },26 {27 accessorKey: 'lastName',28 header: 'Last Name',29 },30 ],31 [],32 );3334 const table = useMaterialReactTable({35 columns,36 data,37 displayColumnDefOptions: {38 'mrt-row-expand': {39 muiTableHeadCellProps: {40 align: 'right',41 },42 muiTableBodyCellProps: {43 align: 'right',44 },45 },46 },47 initialState: { expanded: true },48 renderDetailPanel: ({ row }) => (49 <Box50 sx={{51 display: 'grid',52 margin: 'auto',53 gridTemplateColumns: '1fr 1fr',54 width: '100%',55 }}56 >57 <Typography>Address: {row.original.address}</Typography>58 <Typography>City: {row.original.city}</Typography>59 <Typography>State: {row.original.state}</Typography>60 <Typography>Country: {row.original.country}</Typography>61 </Box>62 ),63 });6465 return <MaterialReactTable table={table} />;66};6768export default Example;69
View Extra Storybook Examples