Selection Feature Guide
Material React Table has a built-in row-selection feature and makes it easy to manage the selection state yourself. This guide will walk you through how to enable row selection and how to customize the selection behavior.
Relevant Props
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| |||
2 |
| ||||
3 |
|
| |||
4 |
|
| |||
5 |
| Material UI Checkbox Props | |||
6 |
| Material UI Checkbox Props | |||
7 |
| TanStack Table Row Selection Docs | |||
8 |
|
| |||
Enable Selection
Selection checkboxes can be enabled with the enableRowSelection
prop.
<MaterialReactTable columns={columns} data={data} enableRowSelection />
Access Selection State
There a couple of ways to access the selection state. You can either manage the selection state yourself or read it from the table instance.
Manage Selection State
The row selection state is managed internally by default, but more than likely, you will want to have access to that state yourself. Here is how you can simply get access to the row selection state, specifically.
const [rowSelection, setRowSelection] = useState({});return (<MaterialReactTablecolumns={columns}data={data}enableRowSelectiononRowSelectionChange={setRowSelection}state={{ rowSelection }}/>);
Read Selection State from Table Instance
Alternatively, you can read the selection state from the tableInstanceRef
ref like so:
const tableInstanceRef = useRef<MRT_TableInstance<YouDataType>>(null); //tsconst someEventHandler = () => {const rowSelection = tableInstanceRef.current.getState().rowSelection;};return (<MaterialReactTablecolumns={columns}data={data}enableRowSelectionrenderTopToolbarCustomActions={() => (<Button onClick={someEventHandler}>{'Do Something with Selected Rows'}</Button>)}tableInstanceRef={tableInstanceRef}/>);
Useful Row IDs
By default, the row.id
for each row in the table is simply the index of the row in the table. You can override this and tell Material React Table to use a more useful Row ID with the getRowId
prop. For example, you may want some like this:
<MaterialReactTablecolumns={columns}data={data}enableRowSelectiongetRowId={(originalRow) => originalRow.userId}/>
Now as rows get selected, the rowSelection
state will look like this:
{"3f25309c-8fa1-470f-811e-cdb082ab9017": true,"be731030-df83-419c-b3d6-9ef04e7f4a9f": true,...}
This can be very useful when you are trying to read your selection state and do something with your data as the row selection changes.
First Name | Last Name | Age | Address | City | State | |
---|---|---|---|---|---|---|
Dylan | Murray | 22 | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 18 | 769 Dominic Grove | Columbus | Ohio |
1import React, { FC, useEffect, useMemo, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { RowSelectionState } from '@tanstack/react-table';45const data = [6 {7 userId: '3f25309c-8fa1-470f-811e-cdb082ab9017', //we'll use this as a unique row id8 firstName: 'Dylan',9 lastName: 'Murray',10 age: 22,11 address: '261 Erdman Ford',12 city: 'East Daphne',13 state: 'Kentucky',14 }, //data definitions...25];2627const Example: FC = () => {28 const columns = useMemo(29 //column definitions...58 );5960 //optionally, you can manage the row selection state yourself61 const [rowSelection, setRowSelection] = useState<RowSelectionState>({});6263 useEffect(() => {64 //do something when the row selection changes...65 console.info({ rowSelection });66 }, [rowSelection]);6768 return (69 <MaterialReactTable70 columns={columns}71 data={data}72 enableRowSelection73 getRowId={(row) => row.userId} //give each row a more useful id74 onRowSelectionChange={setRowSelection} //connect internal row selection state to your own75 state={{ rowSelection }} //pass our managed row selection state to the table to use76 />77 );78};7980export default Example;81
Select Row on Row Click
By default, a row can only be selected by either clicking the checkbox or radio button in the mrt-row-select
column. If you want to be able to select a row by clicking anywhere on the row, you can add your own onClick
function to a table body row like this:
<MaterialReactTablecolumns={columns}data={data}enableRowSelection//clicking anywhere on the row will select itmuiTableBodyRowProps={({ row }) => ({onClick: row.getToggleSelectedHandler(),sx: { cursor: 'pointer' },})}/>
Disable Select All
By default, if you enable selection for each row, there will also be a select all checkbox in the header of the checkbox column. It can be hidden with the enableSelectAll
prop.
<MaterialReactTablecolumns={columns}data={data}enableRowSelectionenableSelectAll={false}/>
Single Row Selection
New in
v1.1
!
By default, the enableMultiRowSelection
prop is set to true
, which means that multiple rows can be selected at once with a checkbox. If you want to only allow a single row to be selected at a time, you can set this prop to false
and a radio button will be used instead of a checkbox.
<MaterialReactTablecolumns={columns}data={data}enableMultiRowSelection={false} //shows radio buttons instead of checkboxesenableRowSelection/>
First Name | Last Name | Age | Address | City | State | |
---|---|---|---|---|---|---|
Dylan | Murray | 22 | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 18 | 769 Dominic Grove | Columbus | Ohio |
1import React, { FC, useMemo, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { RowSelectionState } from '@tanstack/react-table';45const data = [6 //data definitions...26];2728const Example: FC = () => {29 const columns = useMemo(30 //column definitions...59 );6061 //optionally, you can manage the row selection state yourself62 const [rowSelection, setRowSelection] = useState<RowSelectionState>({});6364 return (65 <MaterialReactTable66 columns={columns}67 data={data}68 enableMultiRowSelection={false} //use radio buttons instead of checkboxes69 enableRowSelection70 getRowId={(row) => row.userId} //give each row a more useful id71 muiTableBodyRowProps={({ row }) => ({72 //add onClick to row to select upon clicking anywhere in the row73 onClick: row.getToggleSelectedHandler(),74 sx: { cursor: 'pointer' },75 })}76 onRowSelectionChange={setRowSelection} //connect internal row selection state to your own77 state={{ rowSelection }} //pass our managed row selection state to the table to use78 />79 );80};8182export default Example;83
Customize Select Checkboxes or Radio Buttons
The selection checkboxes can be customized with the muiSelectCheckboxProps
prop. Any prop that can be passed to a Mui Checkbox component can be specified here. For example, you may want to use a different color for the checkbox, or use some logic to disable certain rows from being selected.
<MaterialReactTablecolumns={columns}data={data}enableRowSelectionmuiSelectCheckboxProps={{color: 'secondary',}}/>
First Name | Last Name | Age | Address | City | State | |
---|---|---|---|---|---|---|
Dylan | Murray | 22 | 261 Erdman Ford | East Daphne | Kentucky | |
Raquel | Kohler | 18 | 769 Dominic Grove | Columbus | Ohio | |
Ervin | Reinger | 20 | 566 Brakus Inlet | South Linda | West Virginia | |
Brittany | McCullough | 21 | 722 Emie Stream | Lincoln | Nebraska | |
Branson | Frami | 32 | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';34const Example: FC = () => {5 const columns = useMemo(6 () =>7 [8 //column definitions...34 ] as MRT_ColumnDef<typeof data[0]>[],35 [],36 );3738 const data = useMemo(39 () => [40 //data definitions...82 ],83 [],84 );85 return (86 <MaterialReactTable87 columns={columns}88 data={data}89 enableSelectAll={false}90 enableRowSelection91 muiSelectCheckboxProps={({ row }) => ({92 color: 'secondary',93 disabled: row.getValue<number>('age') < 21,94 })}95 />96 );97};9899export default Example;100
Manual Row Selection Without Checkboxes
You may have a use case where you want to be able to select rows by clicking them, but you don't want to show any checkboxes or radio buttons. You can do this by implementing a row selection feature yourself, while keeping the enableRowSelection
prop false
so that the default selection behavior is disabled.
First Name | Last Name | Age | Address | City | State |
---|---|---|---|---|---|
Dylan | Murray | 22 | 261 Erdman Ford | East Daphne | Kentucky |
Raquel | Kohler | 18 | 769 Dominic Grove | Columbus | Ohio |
1import React, { FC, useEffect, useMemo, useState } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { RowSelectionState } from '@tanstack/react-table';45const data = [6 //data definitions...26];2728const Example: FC = () => {29 const columns = useMemo<MRT_ColumnDef<typeof data[0]>[]>(30 //column definitions...58 );5960 const [rowSelection, setRowSelection] = useState<RowSelectionState>({});6162 return (63 <MaterialReactTable64 columns={columns}65 data={data}66 getRowId={(row) => row.userId}67 muiTableBodyRowProps={({ row }) => ({68 //implement row selection click events manually69 onClick: () =>70 setRowSelection((prev) => ({71 ...prev,72 [row.id]: !prev[row.id],73 })),74 selected: rowSelection[row.id],75 sx: {76 cursor: 'pointer',77 },78 })}79 state={{ rowSelection }}80 />81 );82};8384export default Example;85
View Extra Storybook Examples