Theme

Examples › Default column properties

If you find yourself repeating the same properties over and over again for multiple columns, you can use defaultColumnProps (which accepts a subset of column properties) to set them once and use as a fallback for all columns:
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { DataTable } from 'mantine-datatable';
dayjs.extend(relativeTime);
export default function DefaultColumnPropertiesExample() {
return (
<DataTable
withBorder
withColumnBorders
striped
records={[
{ id: 1, bookTitle: 'The Lord of the Rings', published: new Date(1954, 6, 29), pages: 1178, chapters: 37 },
// more records...
]}
columns={[
{ accessor: 'bookTitle', width: '100%', textAlignment: 'left' },
{
accessor: 'published',
render: ({ published }) => dayjs(published).fromNow(),
},
{ accessor: 'pages' },
{ accessor: 'chapters' },
]}
defaultColumnProps={{
textAlignment: 'right',
noWrap: true,
ellipsis: true,
cellsSx: { fontStyle: 'italic' },
titleSx: (theme) => ({ '&&': { color: theme.colors.green[6] } }),
}}
/>
);
}
The code above will produce the following result:
The Lord of the Rings69 years ago117837
The Hobbit86 years ago31019
The Silmarillion46 years ago36524
The Children of Húrin16 years ago31314
The Fall of Gondolin5 years ago3043
The Lay of Aotrou and Itroun4 years ago1281
The Lays of Beleriand4 years ago4002
The Book of Lost Tales, Part One39 years ago2972
The Book of Lost Tales, Part Two39 years ago2972
No records
In certain scenarios, you can also use a render fallback function. Head over to the next example to learn more.

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