Examples › Records selection
In order to enable records selection, you’ll have to add the following properties to the DataTable:
selectedRecords
→ an array of currently selected records (with the same TS type as therecords
property);onSelectedRecordsChange
→ a callback that will be invoked when the user alters the current selection.
When adding these two properties, the component will render a selection checkbox column and handle user input as following:
- Clicking a row selection checkbox will result in selecting/deselecting the underlying record;
- Clicking the column header checkbox will result in selecting/deselecting all visible records;
- Shift-clicking a row selection checkbox will result in intuitively selecting all records between the last clicked record and the current one.
Disable selection of certain records
You can disable the selection of certain records by providing an `isRecordSelectable` property like so:
The above code will result in the following behavior:
Customizing the selection column className and style
By default, the selection column has an absolute width of 0
, to that it only takes up the space required by the selection checkboxes. If you’re not happy with this behavior, or you have other reasons to customize the column’s properties, you can do so by providing the selectionColumnClassName
and/or selectionColumnStyle
properties:
Additional selection checkbox props
You can pass additional props to the selection checkboxes by providing the following properties:
checkboxProps
→ an object of props that will be applied to all selection checkboxes (both the column header checkbox and the row selection checkboxes);allRecordsSelectionCheckboxProps
→ an object of props that will be applied to the column header checkbox;getRecordSelectionCheckboxProps
→ a function that receives the underlying record and record index and returns an object of props that will be applied to the row selection checkboxes.
In this example, the checkboxes will be rendered with a smaller size and will have custom aria-label
attributes that will be read by screen readers (inspect the DOM to check these):
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 will only select/deselect the records on the currently visible page.
However, in certain you might want to give users the ability to “select all records on all pages” (like you have in Gmail’s user interface). In this case, you can use the allRecordsSelectionCheckboxProps
to create your own selection mechanism:
You have selected 0 records of a total of 500.
Here is the code for the above example:
Horizontal scrolling behavior
Notice how, when the table needs to be horizontally scrollable, the selection column will be fixed to the left side of the table, so that the selection checkboxes are always visible:
Code:
Maximizing the selection trigger area to the entire cell
By default, selection is triggered when the user clicks the row selection checkbox.
However, you can maximize the trigger area to the entire cell holding the checkbox by setting the selectionTrigger
property value to cell
:
In this case, clicking anywhere in the cell will result in selecting/deselecting the underlying record:
Head over to the next example to discover more features.