MRT logoMaterial React Table

Legacy V1 Docs
On This Page

    Row 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 demonstrates how to enable row selection and customize the selection behavior.

    Relevant Props

    1
    boolean
    true
    MRT Row Selection Docs
    2
    boolean | (row: MRT_Row) => boolean
    3
    boolean
    true
    4
    boolean
    true
    5
    (originalRow: TData, index: number, parent?: MRT_Row<TData>) => string
    TanStack Table Core Table Docs
    6
    CheckboxProps | ({ table }) => CheckboxProps
    Material UI Checkbox Props
    7
    CheckboxProps | ({ row, table }) => CheckboxProps
    Material UI Checkbox Props
    8
    OnChangeFn<RowSelectionState>
    TanStack Table Row Selection Docs
    9
    'bottom' | 'top' | 'none'
    10
    'all' | 'page'
    'page'

    Relevant State

    1
    Record<string, boolean>
    {}
    TanStack Table Row Selection Docs

    Enable Row Selection

    Selection checkboxes can be enabled with the enableRowSelection prop.

    <MaterialReactTable columns={columns} data={data} enableRowSelection />

    Enable Row Selection Conditionally Per Row

    You can also enable row selection conditionally per row with the same enableRowSelection prop, but with a callback function instead of a boolean.

    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection={(row) => row.original.age > 18} //disable row selection for rows with age <= 18
    />

    Access Row Selection State

    There are two ways to access the selection state. You can either manage the selection state yourself or read it from the table instance.

    Manage Row Selection State

    The row selection state is managed internally by default, but you will more than likely 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 (
    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection
    onRowSelectionChange={setRowSelection}
    state={{ rowSelection }}
    />
    );

    Read Row Selection State from Table Instance

    Alternatively, you can read the selection state from the tableInstanceRef ref as follows:

    const tableInstanceRef = useRef<MRT_TableInstance<YouDataType>>(null); //ts
    const someEventHandler = () => {
    const rowSelection = tableInstanceRef.current.getState().rowSelection;
    };
    return (
    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection
    renderTopToolbarCustomActions={() => (
    <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 instruct Material React Table to use a more useful Row ID with the getRowId prop. For example, you may want something like this:

    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection
    getRowId={(originalRow) => originalRow.userId}
    />

    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.


    Demo

    Open StackblitzOpen Code SandboxOpen on GitHub
    DylanMurray22261 Erdman FordEast DaphneKentucky
    RaquelKohler18769 Dominic GroveColumbusOhio

    Rows per page

    1-2 of 2

    Source Code

    1import React, { useEffect, useMemo, useState } from 'react';
    2import {
    3 MaterialReactTable,
    4 type MRT_ColumnDef,
    5 type MRT_RowSelectionState,
    6} from 'material-react-table';
    7
    8const data = [
    9 {
    10 userId: '3f25309c-8fa1-470f-811e-cdb082ab9017', //we'll use this as a unique row id
    11 firstName: 'Dylan',
    12 lastName: 'Murray',
    13 age: 22,
    14 address: '261 Erdman Ford',
    15 city: 'East Daphne',
    16 state: 'Kentucky',
    17 }, //data definitions...
    28];
    29
    30const Example = () => {
    31 const columns = useMemo(
    32 //column definitions...
    61 );
    62
    63 //optionally, you can manage the row selection state yourself
    64 const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
    65
    66 useEffect(() => {
    67 //do something when the row selection changes...
    68 console.info({ rowSelection });
    69 }, [rowSelection]);
    70
    71 return (
    72 <MaterialReactTable
    73 columns={columns}
    74 data={data}
    75 enableRowSelection
    76 getRowId={(row) => row.userId} //give each row a more useful id
    77 onRowSelectionChange={setRowSelection} //connect internal row selection state to your own
    78 state={{ rowSelection }} //pass our managed row selection state to the table to use
    79 />
    80 );
    81};
    82
    83export default Example;
    84

    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:

    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection
    //clicking anywhere on the row will select it
    muiTableBodyRowProps={({ 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.

    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection
    enableSelectAll={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.

    <MaterialReactTable
    columns={columns}
    data={data}
    enableMultiRowSelection={false} //shows radio buttons instead of checkboxes
    enableRowSelection
    />

    Demo

    DylanMurray22261 Erdman FordEast DaphneKentucky
    RaquelKohler18769 Dominic GroveColumbusOhio

    Rows per page

    1-2 of 2

    Source Code

    1import React, { useMemo, useState } from 'react';
    2import {
    3 MaterialReactTable,
    4 type MRT_ColumnDef,
    5 type MRT_RowSelectionState,
    6} from 'material-react-table';
    7
    8const data = [
    9 //data definitions...
    29];
    30
    31const Example = () => {
    32 const columns = useMemo(
    33 //column definitions...
    62 );
    63
    64 //optionally, you can manage the row selection state yourself
    65 const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
    66
    67 return (
    68 <MaterialReactTable
    69 columns={columns}
    70 data={data}
    71 enableMultiRowSelection={false} //use radio buttons instead of checkboxes
    72 enableRowSelection
    73 getRowId={(row) => row.userId} //give each row a more useful id
    74 muiTableBodyRowProps={({ row }) => ({
    75 //add onClick to row to select upon clicking anywhere in the row
    76 onClick: row.getToggleSelectedHandler(),
    77 sx: { cursor: 'pointer' },
    78 })}
    79 onRowSelectionChange={setRowSelection} //connect internal row selection state to your own
    80 state={{ rowSelection }} //pass our managed row selection state to the table to use
    81 />
    82 );
    83};
    84
    85export default Example;
    86

    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.

    <MaterialReactTable
    columns={columns}
    data={data}
    enableRowSelection
    muiSelectCheckboxProps={{
    color: 'secondary',
    }}
    />

    Demo

    DylanMurray22261 Erdman FordEast DaphneKentucky
    RaquelKohler18769 Dominic GroveColumbusOhio
    ErvinReinger20566 Brakus InletSouth LindaWest Virginia
    BrittanyMcCullough21722 Emie StreamLincolnNebraska
    BransonFrami3232188 Larkin TurnpikeCharlestonSouth Carolina

    Rows per page

    1-5 of 5

    Source Code

    1import React, { useMemo } from 'react';
    2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';
    3import { data } from './makeData';
    4
    5const Example = () => {
    6 const columns = useMemo(
    7 () =>
    8 [
    9 //column definitions...
    35 ] as MRT_ColumnDef<(typeof data)[0]>[],
    36 [],
    37 );
    38
    39 return (
    40 <MaterialReactTable
    41 columns={columns}
    42 data={data}
    43 enableSelectAll={false}
    44 enableRowSelection={(row) => row.original.age >= 21} //enable row selection conditionally per row
    45 muiSelectCheckboxProps={{ color: 'secondary' }}
    46 />
    47 );
    48};
    49
    50export default Example;
    51

    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 do not 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.


    Demo

    DylanMurray22261 Erdman FordEast DaphneKentucky
    RaquelKohler18769 Dominic GroveColumbusOhio

    Rows per page

    1-2 of 2

    Source Code

    1import React, { useEffect, useMemo, useState } from 'react';
    2import {
    3 MaterialReactTable,
    4 type MRT_ColumnDef,
    5 type MRT_RowSelectionState,
    6} from 'material-react-table';
    7
    8const data = [
    9 //data definitions...
    29];
    30
    31const Example = () => {
    32 const columns = useMemo<MRT_ColumnDef<(typeof data)[0]>[]>(
    33 //column definitions...
    61 );
    62
    63 const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
    64
    65 return (
    66 <MaterialReactTable
    67 columns={columns}
    68 data={data}
    69 getRowId={(row) => row.userId}
    70 muiTableBodyRowProps={({ row }) => ({
    71 //implement row selection click events manually
    72 onClick: () =>
    73 setRowSelection((prev) => ({
    74 ...prev,
    75 [row.id]: !prev[row.id],
    76 })),
    77 selected: rowSelection[row.id],
    78 sx: {
    79 cursor: 'pointer',
    80 },
    81 })}
    82 state={{ rowSelection }}
    83 />
    84 );
    85};
    86
    87export default Example;
    88

    View Extra Storybook Examples