Examples › Default column render

If you provide a defaultColumnRender function to the DataTable, it will be used to render the cells of all columns that don’t have their own render function.
The defaultColumnRender function receives the current record, its index in records and the column accessor as arguments, and should return a ReactNode.

In the example below, a single function renders strings as they are and dates as relative time intervals, so the columns don’t need individual render functions:

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

Here is the code for the above example:

'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();
      }}
    />
  );
}

Head over to the next example to learn more.

Mantine DataTable is used and trusted by