Theme

Examples › Default column render

If you provide a defaultColumnRender prop to the table, it will be used to render all columns that do not provide a custom render function.
The defaultColumnRender function receives the current row, its index and accessor name and should return a ReactNode:
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { DataTable } from 'mantine-datatable';
dayjs.extend(relativeTime);
export default function DefaultColumnRenderExample() {
return (
<DataTable
withBorder
withColumnBorders
striped
records={[
{ bookTitle: 'The Lord of the Rings', published: new Date(1954, 6, 29) },
// more records...
]}
idAccessor="bookTitle"
columns={[
{ accessor: 'bookTitle', width: '100%' },
{ accessor: 'published', textAlignment: 'right', ellipsis: true },
]}
defaultColumnRender={(row, _, accessor) => {
const data = row[accessor as keyof typeof row];
return typeof data === 'string' ? data : dayjs(data).fromNow();
}}
/>
);
}
The code above will produce the following result:
The Lord of the Rings69 years ago
The Hobbit86 years ago
The Silmarillion46 years ago
The Children of Húrin16 years ago
The Fall of Gondolin5 years ago
The Lay of Aotrou and Itroun4 years ago
The Lays of Beleriand4 years ago
The Book of Lost Tales, Part One39 years ago
The Book of Lost Tales, Part Two39 years ago
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