MRT logoMantine React Table

On This Page

    Row Selection Feature Guide

    Mantine 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 Table Options

    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
    Mantine Checkbox Docs
    7
    CheckboxProps | ({ row, table }) => CheckboxProps
    Mantine Checkbox Docs
    8
    OnChangeFn<RowSelectionState>
    TanStack Table Row Selection Docs
    9
    'bottom' | 'top' | 'head-overlay' | 'none'
    10
    'all' | 'page'
    'page'
    11
    'checkbox' | 'radio' | 'switch'
    'checkbox'
    MRT Editing Docs

    Relevant State

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

    Enable Row Selection

    Selection checkboxes can be enabled with the enableRowSelection table option.
    const table = useMantineReactTable({ columns, data, enableRowSelection: true });

    Enable Row Selection Conditionally Per Row

    You can also enable row selection conditionally per row with the same enableRowSelection table option, but with a callback function instead of a boolean.
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: (row) => row.original.age > 18, //disable row selection for rows with age <= 18
    });

    Access Row 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 Row 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<MRT_RowSelectionState>({}); //ts type available
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    onRowSelectionChange: setRowSelection,
    state: { rowSelection },
    });
    return <MantineReactTable table={table} />;

    Read Row Selection State or rows from Table Instance

    Alternatively, you can read the selection state directly from the table instance like so:
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    renderTopToolbarCustomActions: ({ table }) => (
    <Button
    onClick={() => {
    const rowSelection = table.getState().rowSelection; //read state
    const selectedRows = table.getSelectedRowModel().rows; //or read entire rows
    }}
    >
    Download Selected Users
    </Button>
    ),
    });
    useEffect(() => {
    //fetch data based on row selection state or something
    }, [table.getState().rowSelection]);
    return <MantineReactTable table={table} />;

    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 Mantine React Table to use a more useful Row ID with the getRowId table option. For example, you may want some like this:
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    getRowId: (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.
    DylanMurray22261 Erdman FordEast DaphneKentucky
    RaquelKohler18769 Dominic GroveColumbusOhio
    1-2 of 2
    1
    import { useEffect, useMemo, useState } from 'react';
    2
    import {
    3
    MantineReactTable,
    4
    useMantineReactTable,
    5
    type MRT_ColumnDef,
    6
    type MRT_RowSelectionState,
    7
    } from 'mantine-react-table';
    8
    9
    const data = [
    10
    {
    11
    userId: '3f25309c-8fa1-470f-811e-cdb082ab9017', //we'll use this as a unique row id
    12
    firstName: 'Dylan',
    13
    lastName: 'Murray',
    14
    age: 22,
    15
    address: '261 Erdman Ford',
    16
    city: 'East Daphne',
    17
    state: 'Kentucky',
    18
    },
    19
    {
    20
    userId: 'be731030-df83-419c-b3d6-9ef04e7f4a9f',
    21
    firstName: 'Raquel',
    22
    lastName: 'Kohler',
    23
    age: 18,
    24
    address: '769 Dominic Grove',
    25
    city: 'Columbus',
    26
    state: 'Ohio',
    27
    },
    28
    ];
    29
    30
    const Example = () => {
    31
    const columns = useMemo(
    32
    () =>
    33
    [
    34
    {
    35
    accessorKey: 'firstName',
    36
    header: 'First Name',
    37
    },
    38
    {
    39
    accessorKey: 'lastName',
    40
    header: 'Last Name',
    41
    },
    42
    {
    43
    accessorKey: 'age',
    44
    header: 'Age',
    45
    },
    46
    {
    47
    accessorKey: 'address',
    48
    header: 'Address',
    49
    },
    50
    {
    51
    accessorKey: 'city',
    52
    header: 'City',
    53
    },
    54
    {
    55
    accessorKey: 'state',
    56
    header: 'State',
    57
    },
    58
    ] as MRT_ColumnDef<(typeof data)[0]>[],
    59
    [],
    60
    );
    61
    62
    //optionally, you can manage the row selection state yourself
    63
    const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
    64
    65
    useEffect(() => {
    66
    //do something when the row selection changes...
    67
    console.info({ rowSelection });
    68
    }, [rowSelection]);
    69
    70
    const table = useMantineReactTable({
    71
    columns,
    72
    data,
    73
    enableRowSelection: true,
    74
    getRowId: (row) => row.userId,
    75
    onRowSelectionChange: setRowSelection,
    76
    state: { rowSelection },
    77
    });
    78
    79
    return <MantineReactTable table={table} />;
    80
    };
    81
    82
    export default Example;
    1
    import { useEffect, useMemo, useState } from 'react';
    2
    import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';
    3
    4
    const data = [
    5
    {
    6
    userId: '3f25309c-8fa1-470f-811e-cdb082ab9017', //we'll use this as a unique row id
    7
    firstName: 'Dylan',
    8
    lastName: 'Murray',
    9
    age: 22,
    10
    address: '261 Erdman Ford',
    11
    city: 'East Daphne',
    12
    state: 'Kentucky',
    13
    },
    14
    {
    15
    userId: 'be731030-df83-419c-b3d6-9ef04e7f4a9f',
    16
    firstName: 'Raquel',
    17
    lastName: 'Kohler',
    18
    age: 18,
    19
    address: '769 Dominic Grove',
    20
    city: 'Columbus',
    21
    state: 'Ohio',
    22
    },
    23
    ];
    24
    25
    const Example = () => {
    26
    const columns = useMemo(
    27
    () => [
    28
    {
    29
    accessorKey: 'firstName',
    30
    header: 'First Name',
    31
    },
    32
    {
    33
    accessorKey: 'lastName',
    34
    header: 'Last Name',
    35
    },
    36
    {
    37
    accessorKey: 'age',
    38
    header: 'Age',
    39
    },
    40
    {
    41
    accessorKey: 'address',
    42
    header: 'Address',
    43
    },
    44
    {
    45
    accessorKey: 'city',
    46
    header: 'City',
    47
    },
    48
    {
    49
    accessorKey: 'state',
    50
    header: 'State',
    51
    },
    52
    ],
    53
    [],
    54
    );
    55
    56
    //optionally, you can manage the row selection state yourself
    57
    const [rowSelection, setRowSelection] = useState({});
    58
    59
    useEffect(() => {
    60
    //do something when the row selection changes...
    61
    console.info({ rowSelection });
    62
    }, [rowSelection]);
    63
    64
    const table = useMantineReactTable({
    65
    columns,
    66
    data,
    67
    enableRowSelection: true,
    68
    getRowId: (row) => row.userId,
    69
    onRowSelectionChange: setRowSelection,
    70
    state: { rowSelection },
    71
    });
    72
    73
    return <MantineReactTable table={table} />;
    74
    };
    75
    76
    export default Example;

    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:
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    //clicking anywhere on the row will select it
    mantineTableBodyRowProps: ({ 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 table option.
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    enableSelectAll: false,
    });

    Position or Hide Alert Banner Selection Message

    By default, an alert banner will be show as rows are selected. You can use the positionToolbarAlertBanner table option to show the alert banner in the top toolbar, bottom toolbar, as an overlay in the table head, or hide it completely.
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    positionToolbarAlertBanner: 'head-overlay', //show alert banner in bottom toolbar instead of top
    });

    Alternate Switch and Radio Variants

    By default, the selection inputs are Mantine checkboxes, but you can use switch or radio buttons instead with the selectDisplayMode table option.
    DylanMurray22
    RaquelKohler18
    1-2 of 2
    1
    import { useMemo } from 'react';
    2
    import {
    3
    MantineReactTable,
    4
    useMantineReactTable,
    5
    type MRT_ColumnDef,
    6
    } from 'mantine-react-table';
    7
    import { type Person, data } from './makeData';
    8
    9
    const Example = () => {
    10
    const columns = useMemo<MRT_ColumnDef<Person>[]>(
    11
    () => [
    12
    {
    13
    accessorKey: 'firstName',
    14
    header: 'First Name',
    15
    },
    16
    {
    17
    accessorKey: 'lastName',
    18
    header: 'Last Name',
    19
    },
    20
    {
    21
    accessorKey: 'age',
    22
    header: 'Age',
    23
    },
    24
    ],
    25
    [],
    26
    );
    27
    28
    const table = useMantineReactTable({
    29
    columns,
    30
    data,
    31
    enableRowSelection: true,
    32
    getRowId: (row) => row.userId,
    33
    positionToolbarAlertBanner: 'bottom',
    34
    selectDisplayMode: 'switch',
    35
    });
    36
    37
    return <MantineReactTable table={table} />;
    38
    };
    39
    40
    export default Example;
    1
    import { useMemo } from 'react';
    2
    import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';
    3
    import { data } from './makeData';
    4
    5
    const Example = () => {
    6
    const columns = useMemo(
    7
    () => [
    8
    {
    9
    accessorKey: 'firstName',
    10
    header: 'First Name',
    11
    },
    12
    {
    13
    accessorKey: 'lastName',
    14
    header: 'Last Name',
    15
    },
    16
    {
    17
    accessorKey: 'age',
    18
    header: 'Age',
    19
    },
    20
    ],
    21
    [],
    22
    );
    23
    24
    const table = useMantineReactTable({
    25
    columns,
    26
    data,
    27
    enableRowSelection: true,
    28
    getRowId: (row) => row.userId,
    29
    positionToolbarAlertBanner: 'bottom',
    30
    selectDisplayMode: 'switch',
    31
    });
    32
    33
    return <MantineReactTable table={table} />;
    34
    };
    35
    36
    export default Example;

    Single Row Selection

    By default, the enableMultiRowSelection table option 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 table option to false and a radio button will be used instead of a checkbox.
    const table = useMantineReactTable({
    columns,
    data,
    enableMultiRowSelection: false, //shows radio buttons instead of checkboxes
    enableRowSelection: true,
    positionToolbarAlertBanner: 'none', //hide alert banner selection message
    });
    DylanMurray22
    RaquelKohler18
    1-2 of 2
    1
    import { useMemo, useState } from 'react';
    2
    import {
    3
    MantineReactTable,
    4
    useMantineReactTable,
    5
    type MRT_ColumnDef,
    6
    type MRT_RowSelectionState,
    7
    } from 'mantine-react-table';
    8
    import { data, type Person } from './makeData';
    9
    10
    const Example = () => {
    11
    const columns = useMemo<MRT_ColumnDef<Person>[]>(
    12
    () => [
    13
    {
    14
    accessorKey: 'firstName',
    15
    header: 'First Name',
    16
    },
    17
    {
    18
    accessorKey: 'lastName',
    19
    header: 'Last Name',
    20
    },
    21
    {
    22
    accessorKey: 'age',
    23
    header: 'Age',
    24
    },
    25
    ],
    26
    [],
    27
    );
    28
    29
    //optionally, you can manage the row selection state yourself
    30
    const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
    31
    32
    const table = useMantineReactTable({
    33
    columns,
    34
    data,
    35
    enableMultiRowSelection: false, //use radio buttons instead of checkboxes
    36
    enableRowSelection: true,
    37
    positionToolbarAlertBanner: 'none', //don't show the toolbar alert banner
    38
    getRowId: (row) => row.userId, //give each row a more useful id
    39
    mantineTableBodyRowProps: ({ row }) => ({
    40
    //add onClick to row to select upon clicking anywhere in the row
    41
    onClick: row.getToggleSelectedHandler(),
    42
    sx: { cursor: 'pointer' },
    43
    }),
    44
    onRowSelectionChange: setRowSelection, //connect internal row selection state to your own
    45
    state: { rowSelection }, //pass our managed row selection state to the table to use
    46
    });
    47
    48
    return <MantineReactTable table={table} />;
    49
    };
    50
    51
    export default Example;
    1
    import { useMemo, useState } from 'react';
    2
    import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';
    3
    import { data } from './makeData';
    4
    5
    const Example = () => {
    6
    const columns = useMemo(
    7
    () => [
    8
    {
    9
    accessorKey: 'firstName',
    10
    header: 'First Name',
    11
    },
    12
    {
    13
    accessorKey: 'lastName',
    14
    header: 'Last Name',
    15
    },
    16
    {
    17
    accessorKey: 'age',
    18
    header: 'Age',
    19
    },
    20
    ],
    21
    [],
    22
    );
    23
    24
    //optionally, you can manage the row selection state yourself
    25
    const [rowSelection, setRowSelection] = useState({});
    26
    27
    const table = useMantineReactTable({
    28
    columns,
    29
    data,
    30
    enableMultiRowSelection: false, //use radio buttons instead of checkboxes
    31
    enableRowSelection: true,
    32
    positionToolbarAlertBanner: 'none', //don't show the toolbar alert banner
    33
    getRowId: (row) => row.userId, //give each row a more useful id
    34
    mantineTableBodyRowProps: ({ row }) => ({
    35
    //add onClick to row to select upon clicking anywhere in the row
    36
    onClick: row.getToggleSelectedHandler(),
    37
    sx: { cursor: 'pointer' },
    38
    }),
    39
    onRowSelectionChange: setRowSelection, //connect internal row selection state to your own
    40
    state: { rowSelection }, //pass our managed row selection state to the table to use
    41
    });
    42
    43
    return <MantineReactTable table={table} />;
    44
    };
    45
    46
    export default Example;

    Customize Select Checkboxes or Radio Buttons

    The selection checkboxes can be customized with the mantineSelectCheckboxProps table option. Any prop that can be passed to a Mantine 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.
    const table = useMantineReactTable({
    columns,
    data,
    enableRowSelection: true,
    mantineSelectCheckboxProps: {
    color: 'orange',
    },
    });
    DylanMurray22
    RaquelKohler18
    ErvinReinger20
    BrittanyMcCullough21
    BransonFrami32
    1-5 of 5
    1
    import { useMemo } from 'react';
    2
    import {
    3
    MantineReactTable,
    4
    useMantineReactTable,
    5
    type MRT_ColumnDef,
    6
    } from 'mantine-react-table';
    7
    import { data } from './makeData';
    8
    9
    const Example = () => {
    10
    const columns = useMemo(
    11
    () =>
    12
    [
    13
    {
    14
    accessorKey: 'firstName',
    15
    header: 'First Name',
    16
    },
    17
    {
    18
    accessorKey: 'lastName',
    19
    header: 'Last Name',
    20
    },
    21
    {
    22
    accessorKey: 'age',
    23
    header: 'Age',
    24
    },
    25
    ] as MRT_ColumnDef<(typeof data)[0]>[],
    26
    [],
    27
    );
    28
    29
    const table = useMantineReactTable({
    30
    columns,
    31
    data,
    32
    enableSelectAll: false,
    33
    enableRowSelection: (row) => row.original.age >= 21, //enable row selection conditionally per row
    34
    mantineSelectCheckboxProps: { color: 'red', size: 'lg' },
    35
    positionToolbarAlertBanner: 'head-overlay',
    36
    });
    37
    38
    return <MantineReactTable table={table} />;
    39
    };
    40
    41
    export default Example;
    1
    import { useMemo } from 'react';
    2
    import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';
    3
    import { data } from './makeData';
    4
    5
    const Example = () => {
    6
    const columns = useMemo(
    7
    () => [
    8
    {
    9
    accessorKey: 'firstName',
    10
    header: 'First Name',
    11
    },
    12
    {
    13
    accessorKey: 'lastName',
    14
    header: 'Last Name',
    15
    },
    16
    {
    17
    accessorKey: 'age',
    18
    header: 'Age',
    19
    },
    20
    ],
    21
    [],
    22
    );
    23
    24
    const table = useMantineReactTable({
    25
    columns,
    26
    data,
    27
    enableSelectAll: false,
    28
    enableRowSelection: (row) => row.original.age >= 21, //enable row selection conditionally per row
    29
    mantineSelectCheckboxProps: { color: 'red', size: 'lg' },
    30
    positionToolbarAlertBanner: 'head-overlay',
    31
    });
    32
    33
    return <MantineReactTable table={table} />;
    34
    };
    35
    36
    export default Example;

    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 table option false so that the default selection behavior is disabled.
    DylanMurray22261 Erdman FordEast DaphneKentucky
    RaquelKohler18769 Dominic GroveColumbusOhio
    1-2 of 2
    1
    import { useMemo, useState } from 'react';
    2
    import {
    3
    MantineReactTable,
    4
    useMantineReactTable,
    5
    type MRT_ColumnDef,
    6
    type MRT_RowSelectionState,
    7
    } from 'mantine-react-table';
    8
    import { data, type Person } from './makeData';
    9
    10
    const Example = () => {
    11
    const columns = useMemo<MRT_ColumnDef<Person>[]>(
    12
    () => [
    13
    {
    14
    accessorKey: 'firstName',
    15
    header: 'First Name',
    16
    },
    17
    {
    18
    accessorKey: 'lastName',
    19
    header: 'Last Name',
    20
    },
    21
    {
    22
    accessorKey: 'age',
    23
    header: 'Age',
    24
    },
    25
    {
    26
    accessorKey: 'address',
    27
    header: 'Address',
    28
    },
    29
    {
    30
    accessorKey: 'city',
    31
    header: 'City',
    32
    },
    33
    {
    34
    accessorKey: 'state',
    35
    header: 'State',
    36
    },
    37
    ],
    38
    [],
    39
    );
    40
    41
    const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({});
    42
    43
    const table = useMantineReactTable({
    44
    columns,
    45
    data,
    46
    getRowId: (row) => row.userId,
    47
    mantineTableBodyRowProps: ({ row }) => ({
    48
    //implement row selection click events manually
    49
    onClick: () =>
    50
    setRowSelection((prev) => ({
    51
    ...prev,
    52
    [row.id]: !prev[row.id],
    53
    })),
    54
    selected: rowSelection[row.id],
    55
    sx: {
    56
    cursor: 'pointer',
    57
    },
    58
    }),
    59
    state: { rowSelection },
    60
    });
    61
    62
    return <MantineReactTable table={table} />;
    63
    };
    64
    65
    export default Example;
    1
    import { useMemo, useState } from 'react';
    2
    import { MantineReactTable, useMantineReactTable } from 'mantine-react-table';
    3
    import { data } from './makeData';
    4
    5
    const Example = () => {
    6
    const columns = useMemo(
    7
    () => [
    8
    {
    9
    accessorKey: 'firstName',
    10
    header: 'First Name',
    11
    },
    12
    {
    13
    accessorKey: 'lastName',
    14
    header: 'Last Name',
    15
    },
    16
    {
    17
    accessorKey: 'age',
    18
    header: 'Age',
    19
    },
    20
    {
    21
    accessorKey: 'address',
    22
    header: 'Address',
    23
    },
    24
    {
    25
    accessorKey: 'city',
    26
    header: 'City',
    27
    },
    28
    {
    29
    accessorKey: 'state',
    30
    header: 'State',
    31
    },
    32
    ],
    33
    [],
    34
    );
    35
    36
    const [rowSelection, setRowSelection] = useState({});
    37
    38
    const table = useMantineReactTable({
    39
    columns,
    40
    data,
    41
    getRowId: (row) => row.userId,
    42
    mantineTableBodyRowProps: ({ row }) => ({
    43
    //implement row selection click events manually
    44
    onClick: () =>
    45
    setRowSelection((prev) => ({
    46
    ...prev,
    47
    [row.id]: !prev[row.id],
    48
    })),
    49
    selected: rowSelection[row.id],
    50
    sx: {
    51
    cursor: 'pointer',
    52
    },
    53
    }),
    54
    state: { rowSelection },
    55
    });
    56
    57
    return <MantineReactTable table={table} />;
    58
    };
    59
    60
    export default Example;
    View Extra Storybook Examples
    You can help make these docs better! PRs are Welcome
    Using Material-UI instead of Mantine?
    Check out Material React Table