MRT logoMaterial React Table

On This Page

    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

    1
    { [key: string]: MRT_DisplayColumnDef<TData> }
    MRT Display Columns Docs
    2
    'first' | 'last'
    3
    ({ row, table }) => ReactNode

    Relevant State

    1
    Record<string, boolean> | boolean
    {}
    TanStack Table Expanding Docs

    Render Detail Panel

    To add a detail panel to a row, all you need to do is add a renderDetailPanel prop.

    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. Using row.original.columnId is recommended for detail panels since the data in the detail panel usually does not have its own column.

    Demo

    Open StackblitzOpen Code SandboxOpen on GitHub
    1DylanSprouseMurray
    2RaquelHakeemKohler
    3ErvinKrisReinger
    4BrittanyKathrynMcCullough
    5BransonJohnFrami

    1-5 of 5

    Source Code

    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';
    9
    10const 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 );
    34
    35 const table = useMaterialReactTable({
    36 columns,
    37 data,
    38 renderDetailPanel: ({ row }) => (
    39 <Box
    40 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 });
    54
    55 return <MaterialReactTable table={table} />;
    56};
    57
    58export 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 prop. 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 prop to 'last'.

    Demo

    1DylanSprouseMurray

    Address: 261 Erdman Ford

    City: East Daphne

    State: Kentucky

    Country: United States

    2RaquelHakeemKohler

    Address: 769 Dominic Grove

    City: Vancouver

    State: British Columbia

    Country: Canada

    3ErvinKrisReinger

    Address: 566 Brakus Inlet

    City: South Linda

    State: West Virginia

    Country: United States

    1-3 of 3

    Source Code

    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';
    9
    10const 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 );
    33
    34 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 <Box
    50 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 });
    64
    65 return <MaterialReactTable table={table} />;
    66};
    67
    68export default Example;
    69

    View Extra Storybook Examples