Theme

Examples › Custom row or cell attributes

Adding custom attributes to table rows or table cells could be useful for testing purposes.
You can add custom attributes to the table rows using the customRowAttributes property. It accepts a function that receives two arguments, record and recordIndex and returns an object with attributes to add to the row.
Similarly, each column can have its own customCellAttributes property that accepts a function that receives two arguments, record and recordIndex and returns an object with attributes to add to the cell.
Inspect the table below to see the attributes generated for each row and cell:
Sipes IncTwin FallsMT
Runolfsdottir - CummerataMissouri CityKY
Johnston LLCHartfordKY
Crist and SonsAttleboroWV
Schmidt and SonsColliervilleAL
No records
return (
<DataTable
withBorder
shadow="xs"
columns={[
{ accessor: 'name' },
{ accessor: 'city', customCellAttributes: ({ city }) => ({ 'data-cy-city': city }) },
{ accessor: 'state', customCellAttributes: ({ state }) => ({ 'data-cy-state': state }) },
]}
records={records}
customRowAttributes={({ id, name }, recordIndex) => ({
'data-cy-id': id,
'data-cy-name': name,
'data-cy-index': recordIndex,
})}
/>
);

Handling middle-clicks

Here is how you could use the customRowAttributes property to handle middle-clicks on table rows:
Sipes IncTwin FallsMT
Runolfsdottir - CummerataMissouri CityKY
Johnston LLCHartfordKY
Crist and SonsAttleboroWV
Schmidt and SonsColliervilleAL
No records
return (
<DataTable
withBorder
shadow="xs"
columns={[{ accessor: 'name' }, { accessor: 'city' }, { accessor: 'state' }]}
records={records}
customRowAttributes={({ name }) => ({
onMouseDown: (e: MouseEvent) => {
if (e.button === 1) {
alert(`Middle-click on row ${name}`);
}
},
})}
/>
);

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