Theme

Examples › Records selection

Sipes Inc280 Rigoberto DivideTwin FallsMT
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Johnston LLC230 Julie LakeHartfordKY
Crist and Sons3387 Blick TurnpikeAttleboroWV
Schmidt and Sons286 Leif LockColliervilleAL
Nicolas Group09622 Marcel PlaceHighlandOR
Kub and Sons8699 Upton FordsEast ProvidenceIN
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Welch - Tremblay31622 Isobel FallOlatheAR
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
No records
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 the records 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.
const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);
return (
<>
<DataTable
withBorder
withColumnBorders
records={companies}
columns={[
{ accessor: 'name', width: '40%' },
{ accessor: 'streetAddress', width: '60%' },
{ accessor: 'city', width: 160 },
{ accessor: 'state', width: 80 },
]}
selectedRecords={selectedRecords}
onSelectedRecordsChange={setSelectedRecords}
/>
<Paper my="xl" py="xl" withBorder radius={0}>
<Center>
<Button
uppercase
leftIcon={<IconTrash size={16} />}
color="red"
disabled={!selectedRecords.length}
onClick={() => showNotification({ color: 'red', message: 'Deleting data is dangerous!' })}
>
{selectedRecords.length
? `Delete ${
selectedRecords.length === 1 ? 'one selected record' : `${selectedRecords.length} selected records`
}`
: 'Select records to delete'}
</Button>
</Center>
</Paper>
</>
);

Disable selection of certain records

You can disable the selection of certain records by providing an `isRecordSelectable` property like so:
const [selectedRecords, setSelectedRecords] = useState<Company[]>([]);
return (
<DataTable
withBorder
withColumnBorders
records={companies}
columns={[
{ accessor: 'name', width: '40%' },
{ accessor: 'streetAddress', width: '60%' },
{ accessor: 'city', width: 160 },
{ accessor: 'state', width: 80 },
]}
selectedRecords={selectedRecords}
onSelectedRecordsChange={setSelectedRecords}
isRecordSelectable={(record) => record.name.length < 14}
/>
);
Sipes Inc280 Rigoberto DivideTwin FallsMT
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Johnston LLC230 Julie LakeHartfordKY
Crist and Sons3387 Blick TurnpikeAttleboroWV
Schmidt and Sons286 Leif LockColliervilleAL
Nicolas Group09622 Marcel PlaceHighlandOR
Kub and Sons8699 Upton FordsEast ProvidenceIN
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Welch - Tremblay31622 Isobel FallOlatheAR
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
No records

Additional selection checkbox props

You can pass additional props to the selection checkboxes by providing the allRecordsSelectionCheckboxProps and getRecordSelectionCheckboxProps properties. The former is an object of props that will be applied to the column header checkbox, while the latter is a function that receives the underlying record and record index and returns an object of props. will be applied to the row selection checkboxes.
<DataTable
// other props...
allRecordsSelectionCheckboxProps={{ 'aria-label': 'Select all records' }}
getRecordSelectionCheckboxProps={(record) => ({ 'aria-label': `Select ${record.name}` })}
/>
Sipes Inc280 Rigoberto DivideTwin FallsMT
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Johnston LLC230 Julie LakeHartfordKY
Crist and Sons3387 Blick TurnpikeAttleboroWV
Schmidt and Sons286 Leif LockColliervilleAL
Nicolas Group09622 Marcel PlaceHighlandOR
Kub and Sons8699 Upton FordsEast ProvidenceIN
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Welch - Tremblay31622 Isobel FallOlatheAR
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
No records

Mantine DataTable is trusted by

MIT LicenseSponsor the author
Built by Ionut-Cristian Florescu and these awesome people.
Please sponsor the project if you find it useful.
GitHub StarsNPM Downloads