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 Rings72 years ago
The Hobbit89 years ago
The Silmarillion49 years ago
The Children of Húrin19 years ago
The Fall of Gondolin8 years ago
The Lay of Aotrou and Itroun7 years ago
The Lays of Beleriand7 years ago
The Book of Lost Tales, Part One42 years ago
The Book of Lost Tales, Part Two42 years ago
No records

Head over to the next example to learn more.