Customized Grouping Example
Here is an advanced example showing off various ways in which the expand column can be customized with column grouping features. See the Column Grouping Guide to learn more.
Groups | First Name | Last Name | Age | Salary | |
---|---|---|---|---|---|
Alabama(3) | |||||
Female(4) | |||||
Female | Thad | Wiegand | 64 | 56146 | |
Female | Reinhold | Reichel | 30 | 30531 | |
Female | Lurline | Koepp | 59 | 10645 | |
Female | Kody | Braun | 38 | 63733 | |
Male(2) | |||||
Male | Alivia | Ledner | 56 | 12591 | |
Male | Danyka | Gleason | 36 | 71238 | |
Nonbinary(1) | |||||
Nonbinary | Lionel | Hartmann | 30 | 58743 | |
Alaska(2) | |||||
Male(4) | |||||
Male | Eloisa | Kohler | 31 | 45801 | |
Male | Kian | Hand | 56 | 81062 | |
Male | Michale | Collier | 59 | 75197 | |
Male | Eldridge | Stroman | 42 | 59594 | |
Female(4) | |||||
Female | Loyce | Schmidt | 29 | 76295 | |
Female | Alvera | Balistreri | 25 | 79844 |
20
1import { useMemo, useState } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6 type MRT_Row,7 MRT_ExpandAllButton,8} from 'material-react-table';9import { data, type Person } from './makeData';10import { Box, Stack } from '@mui/material';1112const Example = () => {13 const columns = useMemo<MRT_ColumnDef<Person>[]>(14 //column definitions...43 );4445 const table = useMaterialReactTable({46 columns,47 data,48 displayColumnDefOptions: {49 'mrt-row-expand': {50 Header: () => (51 <Stack direction="row" alignItems="center">52 <MRT_ExpandAllButton table={table} />53 <Box>Groups</Box>54 </Stack>55 ),56 GroupedCell: ({ row, table }) => {57 const { grouping } = table.getState();58 return row.getValue(grouping[grouping.length - 1]);59 },60 enableResizing: true,61 muiTableBodyCellProps: ({ row }) => ({62 sx: (theme) => ({63 color:64 row.depth === 065 ? theme.palette.primary.main66 : row.depth === 167 ? theme.palette.secondary.main68 : undefined,69 }),70 }),71 size: 200,72 },73 },74 enableGrouping: true,75 enableColumnResizing: true,76 groupedColumnMode: 'remove',77 initialState: {78 density: 'compact',79 expanded: true, //expand all groups by default80 grouping: ['state', 'gender'], //an array of columns to group by by default (can be multiple)81 pagination: { pageIndex: 0, pageSize: 20 },82 sorting: [{ id: 'state', desc: false }],83 },84 });8586 return <MaterialReactTable table={table} />;87};8889export default Example;90
View Extra Storybook Examples