Row Actions Feature Guide
The row actions feature is simply a pre-built Display Column feature that adds a column as a place to store either a row action menu or other row action buttons.
Using the built-in Row Actions column is optional, as you can simply create your own display columns, but this feature has some built-in conveniences that make it easy to add row actions to your table.
Relevant Props
# | Prop Name 2 | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| MRT Display Columns Docs | |||
2 |
| ||||
3 |
| ||||
4 |
| ||||
5 |
| ||||
Enable Row Actions
You can enable the row actions feature by setting the enableRowActions
prop to true
.
You can then add your row action components with either the renderRowActions
or renderRowActionMenuItems
props.
Add Row Actions Menu Items
If you want to embed all of your row actions into a single menu, you can use the renderRowActionMenuItems
prop.
<MaterialReactTableenableRowActionsrenderRowActionMenuItems={({ row }) => [<MenuItem key="edit" onClick={() => console.info('Edit')}>Edit</MenuItem>,<MenuItem key="delete" onClick={() => console.info('Delete')}>Delete</MenuItem>,]}/>
Actions | First Name | Last Name | Address | City | State |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { useMemo } from 'react';2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';3import { data, type Person } from './makeData';4import { MenuItem } from '@mui/material';56export const Example = () => {7 const columns = useMemo<MRT_ColumnDef<Person>[]>(8 //column definitions...33 );3435 return (36 <MaterialReactTable37 columns={columns}38 data={data}39 enableRowActions40 renderRowActionMenuItems={({ row }) => [41 <MenuItem key="edit" onClick={() => console.info('Edit')}>42 Edit43 </MenuItem>,44 <MenuItem key="delete" onClick={() => console.info('Delete')}>45 Delete46 </MenuItem>,47 ]}48 />49 );50};5152export default Example;53
Add Row Action Buttons
If you want to add row action buttons, you can use the renderRowActions
prop.
<MaterialReactTableenableRowActionsrenderRowActions={({ row }) => (<Box><IconButton onClick={() => console.info('Edit')}><EditIcon /></IconButton><IconButton onClick={() => console.info('Delete')}><DeleteIcon /></IconButton></Box>)}/>
Actions | First Name | Last Name | Address | City | State |
---|---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { useMemo, useState } from 'react';2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';3import { Box, IconButton } from '@mui/material';4import {5 Edit as EditIcon,6 Delete as DeleteIcon,7 Email as EmailIcon,8} from '@mui/icons-material';9import { data as initialData, type Person } from './makeData';1011export const Example = () => {12 const columns = useMemo<MRT_ColumnDef<Person>[]>(13 //column definitions...38 );3940 const [data, setData] = useState<Person[]>(initialData);4142 return (43 <MaterialReactTable44 columns={columns}45 data={data}46 enableRowActions47 renderRowActions={({ row, table }) => (48 <Box sx={{ display: 'flex', flexWrap: 'nowrap', gap: '8px' }}>49 <IconButton50 color="primary"51 onClick={() =>52 window.open(53 `mailto:kevinvandy@mailinator.com?subject=Hello ${row.original.firstName}!`,54 )55 }56 >57 <EmailIcon />58 </IconButton>59 <IconButton60 color="secondary"61 onClick={() => {62 table.setEditingRow(row);63 }}64 >65 <EditIcon />66 </IconButton>67 <IconButton68 color="error"69 onClick={() => {70 data.splice(row.index, 1); //assuming simple data table71 setData([...data]);72 }}73 >74 <DeleteIcon />75 </IconButton>76 </Box>77 )}78 />79 );80};8182export default Example;83
Customize Row Actions Column
Change Row Actions Display Column Options
You can customize all column def options for the row actions column, including the column width, header text, and more using the displayColumnDefOptions
prop.
<MaterialReactTablecolumns={columns}data={data}displayColumnDefOptions={{'mrt-row-actions': {header: 'Change Account Settings', //change header textsize: 300, //make actions column wider},}}renderRowActions={({ table }) => (<Box><Button>Button 1</Button><Button>Button 2</Button><Button>Button 3</Button></Box>)}/>
Position Row Actions Column
You can position the row actions column to the left or right (first or last column) of the table using the positionActionsColumn
prop. The default is as the first column.
<MaterialReactTablecolumns={columns}data={data}positionActionsColumn="last"/>