Examples › Records selection

To enable records selection, add the following properties to the DataTable:

  • selectedRecords → an array of currently selected records (of the same type as the items in the records array);
  • onSelectedRecordsChange → a callback invoked with the new array of selected records when the user changes the selection.

With these two properties set, the component renders a selection checkbox column and handles user input as follows:

  • Clicking a row selection checkbox selects or deselects the underlying record;
  • Clicking the column header checkbox selects or deselects all visible records (selected records that are not currently visible, e.g. on other pages, are left untouched);
  • Shift-clicking a row selection checkbox selects or deselects all records between the last clicked record and the current one.
Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
No records
const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);

return (
  <>
    <DataTable
      striped
      highlightOnHover
      withTableBorder
      withColumnBorders
      records={companies}
      columns={columns}
      selectedRecords={selectedRecords}
      onSelectedRecordsChange={setSelectedRecords}
    />
    {/* delete button... */}
  </>
);

Disable selection of certain records

You can disable the selection of certain records by providing an isRecordSelectable function. It receives a record and its index and should return false for records that can’t be selected:

const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);

return (
  <DataTable
    withTableBorder
    withColumnBorders
    records={companies}
    columns={columns}
    selectedRecords={selectedRecords}
    onSelectedRecordsChange={setSelectedRecords}
    isRecordSelectable={(record) => record.name.length < 14}
  />
);

The selection checkboxes of non-selectable records are disabled, and both the column header checkbox and shift-click range selection skip them:

Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
No records

Customizing the selection column className and style

By default, the selection column is 44px wide — the space needed for the checkbox plus the default xs horizontal spacing on each side. Its width and padding follow the table’s horizontalSpacing and verticalSpacing. If you need a different width, or you need to customize the column for other reasons, you can do so by providing the selectionColumnClassName and/or selectionColumnStyle properties:

const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);

return (
  <DataTable
    // other props...
    selectionColumnStyle={{ minWidth: 100 }}
  />
);
Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
No records

Selection column spacing

The selection column uses the same horizontalSpacing and verticalSpacing values as the other columns. Change them below to see how the checkbox cell padding, width and sticky divider follow along:

const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);
const [horizontalSpacing, setHorizontalSpacing] = useState<MantineSize>('xs');
const [verticalSpacing, setVerticalSpacing] = useState<MantineSize>('xs');

return (
  <Stack>
    {/* spacing controls... */}
    <DataTable
      withTableBorder
      withColumnBorders
      records={companies.slice(0, 5)}
      columns={columns}
      selectedRecords={selectedRecords}
      onSelectedRecordsChange={setSelectedRecords}
      horizontalSpacing={horizontalSpacing}
      verticalSpacing={verticalSpacing}
    />
  </Stack>
);

Horizontal spacing

Vertical spacing

Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
No records

Additional selection checkbox props

You can pass additional Mantine Checkbox props to the selection checkboxes with the following properties:

  • selectionCheckboxProps → an object of props applied to all selection checkboxes (both the column header checkbox and the row selection checkboxes);
  • allRecordsSelectionCheckboxProps → an object of props applied to the column header checkbox;
  • getRecordSelectionCheckboxProps → a function receiving a record and its index and returning an object of props applied to that record’s selection checkbox.
const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);

return (
  <DataTable
    // other props...
    selectionCheckboxProps={{ size: 'xs' }}
    allRecordsSelectionCheckboxProps={{ 'aria-label': 'Select all records' }}
    getRecordSelectionCheckboxProps={(record) => ({ 'aria-label': `Select ${record.name}` })}
  />
);

In this example, the checkboxes are rendered with a smaller size and have custom aria-label attributes, which are read by screen readers (inspect the DOM to check them):

Name
Street address
City
State
Feest, Bogan and Herzog21716 Ratke DriveStromanportWY
Cummerata - Kuhlman6389 Dicki StreamSouth GateNH
Goyette Inc8873 Mertz RapidDorthysideID
Runte Inc2996 Ronny MountMcAllenMA
Goldner, Rohan and Lehner632 Broadway AvenueNorth LouieWY
Doyle, Hodkiewicz and O'Connell576 Joyce WaysTyraburghKS
Rau - O'Hara7508 Lansdowne RoadShieldsboroughMI
Tillman - Jacobi57918 Gwendolyn CirclesSheridanportMI
Connelly, Feest and Hodkiewicz7057 Stanley RoadKearaburghCA
Shanahan, Robel and Beier378 Berta CrescentWest GerryKS
Kling - McLaughlin8346 Kertzmann SquareSouth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
Jogi - McLaughlin83462 Shazam StreetNorth JoesphID
No records

Selecting all records on all pages

When using pagination, the best practice is to only load the current page’s records from the server.
This also means that Mantine DataTable can’t — and shouldn’t — know about your entire dataset, so the “select all” checkbox only selects or deselects the records on the current page.

However, in certain cases you might want to give users the ability to “select all records on all pages” (like in Gmail’s user interface). In this case, you can use allRecordsSelectionCheckboxProps to implement your own selection mechanism. The example below also makes use of the uniqBy and differenceBy utility functions exported by Mantine DataTable:

First name
Last name
Email
Department
Company
JeraldHowellJerald.Howell32@yahoo.comIndustrialRunte Inc
KathleenRueckerKathleen_Ruecker@hotmail.comComputersShanahan, Robel and Beier
EricaVolkmanErica.Volkman37@gmail.comToysGoyette Inc
CliffordOberbrunnerClifford.Oberbrunner@hotmail.comAutomotiveRau - O'Hara
AlisonKlingAlison16@gmail.comJeweleryGoyette Inc
SueZiemeSue.Zieme29@hotmail.comBooksCummerata - Kuhlman
FeliciaGleasonFelicia30@yahoo.comShoesDoyle, Hodkiewicz and O'Connell
AlfredoZemlakAlfredo22@yahoo.comGamesRunte Inc
EmilyBartolettiEmily.Bartoletti@gmail.comAutomotiveRau - O'Hara
DeloresReynoldsDelores.Reynolds@yahoo.comIndustrialRunte Inc
1 - 10 / 500
No records

You have selected 0 records of a total of 500.

Here is the code for the above example:

const PAGE_SIZE = 10;

const [page, setPage] = useState(1);
const [records, setRecords] = useState(employees.slice(0, PAGE_SIZE));
const [allRecordsSelected, setAllRecordsSelected] = useState(false);
const [selectedRecords, setSelectedRecords] = useState<Employee[]>([]);
const [unselectedRecords, setUnselectedRecords] = useState<Employee[]>([]);

const handleSelectedRecordsChange = (newSelectedRecords: Employee[]) => {
  if (allRecordsSelected) {
    const recordsToUnselect = records.filter((record) => !newSelectedRecords.includes(record));
    setUnselectedRecords(
      // 👇 `uniqBy` is a utility function provided by Mantine DataTable
      uniqBy([...unselectedRecords, ...recordsToUnselect], (r) => r.id).filter((r) => !newSelectedRecords.includes(r))
    );
  } else {
    setSelectedRecords(newSelectedRecords);
  }
};

const handleAllRecordsSelectionCheckboxChange = () => {
  if (allRecordsSelected) {
    setAllRecordsSelected(false);
    setSelectedRecords([]);
    setUnselectedRecords([]);
  } else {
    setAllRecordsSelected(true);
  }
};

useEffect(() => {
  const from = (page - 1) * PAGE_SIZE;
  const to = from + PAGE_SIZE;
  const currentRecords = employees.slice(from, to);
  setRecords(currentRecords);
  if (allRecordsSelected) {
    setSelectedRecords(
      // 👇 `differenceBy` is a utility function provided by Mantine DataTable
      differenceBy(currentRecords, unselectedRecords, (r) => r.id)
    );
  }
}, [allRecordsSelected, page, unselectedRecords]);

return (
  <>
    <DataTable
      selectedRecords={selectedRecords}
      onSelectedRecordsChange={handleSelectedRecordsChange}
      allRecordsSelectionCheckboxProps={{
        indeterminate: allRecordsSelected && !!unselectedRecords.length,
        checked: allRecordsSelected,
        onChange: handleAllRecordsSelectionCheckboxChange,
        title: allRecordsSelected ? 'Unselect all records' : `Select ${employees.length} records`,
      }}
      records={records}
      totalRecords={employees.length}
      recordsPerPage={PAGE_SIZE}
      page={page}
      onPageChange={(p) => setPage(p)}
      // other table props...
    />
    <Paper p="md" mt="sm" withBorder>
      <Text size="sm" ta="center">
        You have selected {allRecordsSelected ? employees.length - unselectedRecords.length : selectedRecords.length}{' '}
        records of a total of {employees.length}.
      </Text>
    </Paper>
  </>
);

Horizontal scrolling behavior

Notice how, when the table is horizontally scrollable, the selection column stays pinned to the left side of the table, so that the selection checkboxes are always visible:

First name
Last name
Email
Department
Company
Company mission statement
Street address
City
State
JeraldHowellJerald.Howell32@yahoo.comIndustrialRunte IncEngage synergistic infrastructures.2996 Ronny MountMcAllenMA
KathleenRueckerKathleen_Ruecker@hotmail.comComputersShanahan, Robel and BeierSynthesize customized portals.378 Berta CrescentWest GerryKS
EricaVolkmanErica.Volkman37@gmail.comToysGoyette IncProductize front-end web services.8873 Mertz RapidDorthysideID
CliffordOberbrunnerClifford.Oberbrunner@hotmail.comAutomotiveRau - O'HaraInnovate real-time applications.7508 Lansdowne RoadShieldsboroughMI
AlisonKlingAlison16@gmail.comJeweleryGoyette IncProductize front-end web services.8873 Mertz RapidDorthysideID
No records

Code:

const [selectedRecords, setSelectedRecords] = useState<Employee[]>([]);

return (
  <DataTable
    selectedRecords={selectedRecords}
    onSelectedRecordsChange={setSelectedRecords}
    // 👇 these columns take up more horizontal space
    columns={[
      { accessor: 'firstName' },
      { accessor: 'lastName' },
      { accessor: 'email' },
      { accessor: 'department.name', title: 'Department' },
      { accessor: 'department.company.name', title: 'Company', noWrap: true },
      { accessor: 'department.company.missionStatement', title: 'Company mission statement', noWrap: true },
      { accessor: 'department.company.streetAddress', title: 'Street address', noWrap: true },
      { accessor: 'department.company.city', title: 'City', noWrap: true },
      { accessor: 'department.company.state', title: 'State', textAlign: 'right' },
    ]}
    // other table props...
  />
);

Maximizing the selection trigger area to the entire cell

By default, selection is triggered when the user clicks a selection checkbox.
However, you can extend the trigger area to the entire cell holding the checkbox by setting the selectionTrigger property to 'cell' (the default value is 'checkbox'):

<DataTable
  // other props...
  selectionTrigger="cell" // 👈 click anywhere in the cell to select the record
/>

In this case, clicking anywhere in the cell selects or deselects the underlying record:

Name
Address
Mission statement
Feest, Bogan and Herzog
21716 Ratke Drive
Stromanport
WY
Innovate bricks-and-clicks metrics.
Cummerata - Kuhlman
6389 Dicki Stream
South Gate
NH
Harness real-time channels.
Goyette Inc
8873 Mertz Rapid
Dorthyside
ID
Productize front-end web services.
No records

Head over to the next example to discover more features.

Mantine DataTable is used and trusted by