Examples › Default column properties

If you find yourself repeating the same properties over and over again for multiple columns, you can use defaultColumnProps (which accepts a subset of column properties) to set them once and use as a fallback for all columns.

Book title
Published
Pages
Chapters
The Lord of the Rings70 years ago117837
The Hobbit87 years ago31019
The Silmarillion47 years ago36524
The Children of Húrin17 years ago31314
The Fall of Gondolin6 years ago3043
The Lay of Aotrou and Itroun5 years ago1281
The Lays of Beleriand5 years ago4002
The Book of Lost Tales, Part One40 years ago2972
The Book of Lost Tales, Part Two40 years ago2972
5 to 87 years agoμ 399μ 12
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);

const records = [
  { id: 1, bookTitle: 'The Lord of the Rings', published: new Date(1954, 6, 29), pages: 1178, chapters: 37 },
  // more records...
];

const average = (arr: number[]) => arr.reduce((a, b) => a + b, 0) / arr.length;
const minPublishingDateYearsAgo = dayjs().diff(Math.min(...records.map((r) => r.published.valueOf())), 'years');
const maxPublishingDateYearsAgo = dayjs().diff(Math.max(...records.map((r) => r.published.valueOf())), 'years');

export function DefaultColumnPropertiesExample() {
  return (
    <DataTable
      withTableBorder
      withColumnBorders
      striped
      records={records}
      columns={[
        { accessor: 'bookTitle', width: '100%', textAlign: 'left' },
        {
          accessor: 'published',
          render: ({ published }) => dayjs(published).fromNow(),
          footer: `${maxPublishingDateYearsAgo} to ${minPublishingDateYearsAgo} years ago`,
        },
        {
          accessor: 'pages',
          footer: `μ ${Math.round(average(records.map((r) => r.pages)))}`,
        },
        {
          accessor: 'chapters',
          footer: `μ ${Math.round(average(records.map((r) => r.chapters)))}`,
        },
      ]}
      defaultColumnProps={{
        textAlign: 'right',
        noWrap: true,
        ellipsis: true,

        // 👇 customize cell style based on record data and Mantine theme
        cellsStyle: ({ bookTitle }) =>
          bookTitle === 'The Hobbit' ? (theme) => ({ fontStyle: 'italic', color: theme.colors.red[6] }) : undefined,
        // ...or you could simply return this, if you don't use the record data and Mantine theme:
        //    `cellsStyle: () => ({ fontStyle: 'italic', color: '#F00' })`

        // 👇 customize column title style based on Mantine theme
        titleStyle: (theme) => ({ color: theme.colors.green[6] }),
        // ...or you could simply return an object, if you don't use Mantine theme:
        //    `titleStyle: { color: '#0F0' }`

        footerStyle: (theme) => ({ color: theme.colors.blue[6] }),
      }}
    />
  );
}

In certain scenarios, you can also use a render fallback function. Head over to the next example to learn more.