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 Rings70 years ago
The Hobbit87 years ago
The Silmarillion47 years ago
The Children of Húrin17 years ago
The Fall of Gondolin6 years ago
The Lay of Aotrou and Itroun5 years ago
The Lays of Beleriand5 years ago
The Book of Lost Tales, Part One40 years ago
The Book of Lost Tales, Part Two40 years ago
No records

Head over to the next example to learn more.