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:

'use client';

import { DataTable } from 'mantine-datatable';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';

dayjs.extend(relativeTime);

export function DefaultColumnRenderExample() {
  return (
    <DataTable
      withTableBorder
      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', textAlign: '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:

Book title
Published
The Lord of the Rings71 years ago
The Hobbit88 years ago
The Silmarillion48 years ago
The Children of Húrin18 years ago
The Fall of Gondolin7 years ago
The Lay of Aotrou and Itroun6 years ago
The Lays of Beleriand6 years ago
The Book of Lost Tales, Part One41 years ago
The Book of Lost Tales, Part Two41 years ago
No records

Head over to the next example to learn more.