Data Grid - Master detail
Expand your rows to display additional information.
The master detail feature allows expanding a row to display additional information inside a panel.
To use this feature, pass a function to the getDetailPanelContent
prop with the content to be rendered inside the panel.
Any valid React element can be used as the row detail, even another grid.
The height of the detail panel content needs to be provided upfront.
The grid assumes the value of 500px by default however this can be configured by passing a function to the getDetailPanelHeight
prop that returns the required height.
Both props are called with a GridRowParams
object, allowing you to return a different value for each row.
<DataGridPro
getDetailPanelContent={({ row }) => <div>Row ID: {row.id}</div>}
getDetailPanelHeight={({ row }) => 100} // Optional, default is 500px.
/>
To expand a row, click on the +
icon or press Space inside the detail toggle column.
Returning null
or undefined
as the value of getDetailPanelContent
will prevent the respective row from being expanded.
âš Always memoize the function provided to
getDetailPanelContent
andgetDetailPanelHeight
. The grid depends on the referential value of these props to cache their values and optimize the rendering.const getDetailPanelContent = React.useCallback(() => { ... }, []); <DataGridPro getDetailPanelContent={getDetailPanelContent} />
âš Depending on the height of the detail panel, you may see a blank space when scrolling. This is caused by the grid using a lazy approach to update the rendered rows. Set
rowThreshold
to 0 to force new rows to be rendered more often to fill the blank space. Note that this may reduce the performance.<DataGridPro rowThreshold={0} />
Controlling expanded detail panels
To control which rows are expanded, pass a list of row IDs to the detailPanelExpandedRowIds
prop.
Passing a callback to the onDetailPanelExpandedRowIds
prop can be used to detect when a row gets expanded or collapsed.
On the other hand, if you only want to initialize the grid with some rows already expanded, use the initialState
prop as follows:
<DataGridPro initialState={{ detailPanel: { expandedRowIds: [1, 2, 3] } }}>
Using a detail panel as a form
As an alternative to the built-in row editing, a form component can be rendered inside the detail panel, allowing the user to edit the current row values.
The following demo shows integration with react-hook-form, but other form libraries are also supported.
Customizing the detail panel toggle
To change the icon used for the toggle, you can provide a different component for the icon slot as follow:
<DataGridPro
components={{
DetailPanelExpandIcon: CustomExpandIcon,
DetailPanelCollapseIcon: CustomCollapseIcon,
}}
/>
If this is not sufficient, the entire toggle component can be overridden.
To fully customize it, add another column with field: GRID_DETAIL_PANEL_TOGGLE_FIELD
to your set of columns.
The grid will detect that there is already a toggle column defined and it will not add another toggle in the default position.
The new toggle component can be provided via renderCell
in the same as any other column.
By only setting the field
, is up to you to configure the remaining options (e.g. disable the column menu, filtering, sorting).
To already start with a few suggested options configured, spread GRID_DETAIL_PANEL_TOGGLE_COL_DEF
when defining the column.
<DataGridPro
columns={[
{
field: GRID_DETAIL_PANEL_TOGGLE_FIELD,
renderCell: (params) => <CustomDetailPanelToggle {...params}>
},
]}
/>
// or
<DataGridPro
columns={[
{
...GRID_DETAIL_PANEL_TOGGLE_COL_DEF, // Already contains the right field
renderCell: (params) => <CustomDetailPanelToggle {...params}>
},
]}
/>
This approach can also be used to change the location of the toggle column, as shown below.
Note: As any ordinary cell renderer, the
value
prop is also available and it corresponds to the state of the row:true
when expanded andfalse
when collapsed.
Disable detail panel content scroll
By default, the detail panel has a width that is the sum of the widths of all columns.
This means that when a horizontal scrollbar is present, scrolling it will also scroll the panel content.
To avoid this behavior, set the size of the detail panel to the outer size of the grid.
Use apiRef.current.getRootDimensions()
to get the latest dimension values.
Finally, to prevent the panel from scrolling, set position: sticky
and left: 0
.
The following demo shows how this can be achieved. Notice that the toggle column is pinned to make sure that it will always be visible when the grid is scrolled horizontally.
getExpandedDetailPanels()
Returns the rows whose detail panel is open.
Signature:
getExpandedDetailPanels: () => GridRowId[]
setExpandedDetailPanels()
Changes which rows to expand the detail panel.
Signature:
setExpandedDetailPanels: (ids: GridRowId[]) => void
toggleDetailPanel()
Expands or collapses the detail panel of a row.
Signature:
toggleDetailPanel: (id: GridRowId) => void