Display (Built-in) Columns
Display columns are used to display non-data elements in a table. They only require an id
and header
in the column definition. They do not need an accessorKey
or accessorFn
, as they are not meant to connect to your data at all.
Display columns do not have any processing features, such as sorting, filtering, grouping, etc. enabled on them by default.
Built-in MRT Display Columns
Material React Table has a few built-in display columns that are created automatically when certain features are enabled.
mrt-row-drag
- created whenenableRowDragging
orenableRowOrdering
prop aretrue
mrt-row-actions
- created whenenableRowActions
(or sometimes whenenableEditing
) props aretrue
mrt-row-expand
- created whenenableExpanding
,enableGrouping
, orrenderDetailPanel
props aretrue
mrt-row-select
- created whenenableRowSelection
prop istrue
mrt-row-numbers
- created whenenableRowNumbers
prop istrue
Display columns are, for the most part, the same as a data column, except they do not have an accessor to access data. When a display column is created internally by Material React Table, the following options are all set to false by default:
const defaultDisplayColumnDefOptions = {columnDefType: 'display',enableClickToCopy: false,enableColumnActions: false,enableColumnDragging: false,enableColumnFilter: false,enableColumnOrdering: false,enableEditing: false,enableGlobalFilter: false,enableGrouping: false,enableHiding: false,enableResizing: false,enableSorting: false,} as Partial<MRT_ColumnDef>;
All of these values are able to be overridden if needed, and you'll learn about that in the next section down below.
Customize Built-in MRT Display Columns
It is possible to change and override the default behavior of built-in display columns. Whether you want to change the default column width, add some styles, or enable some features, such as column actions or column drag and drop, you can do it with the displayColumnDefOptions
prop.
Display Column Definition Options Prop
Let's say you need to adjust the width of the Actions column to be wide enough to fit all of your action buttons. You could do that as follows:
<MaterialReactTablecolumns={columns}data={data}displayColumnDefOptions={{ 'mrt-row-actions': { size: 300 } }} //change width of actions column to 300pxenableRowActionsrenderRowActions={({ row }) => (<div><Button>Action 1</Button><Button>Action 2</Button><Button>Action 3</Button></div>)}/>
Or maybe you want to enable a feature that is off by default for display columns, such as column ordering or pinning.
<MaterialReactTablecolumns={columns}data={data}displayColumnDefOptions={{'mrt-row-numbers': {enableOrdering: true,enablePinning: true,enableColumnActions: true,},}}enableRowNumbers/>
Here is a full example and demo for customizing display columns.
Actions | # | First Name | Last Name | Address | City | State | |
---|---|---|---|---|---|---|---|
1 | Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky | ||
2 | Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio | ||
3 | Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia | ||
4 | Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska | ||
5 | Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
1import React, { FC, useMemo } from 'react';2import { Box, Button } from '@mui/material';3import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';4import { data, Person } from './makeData';56const Example: FC = () => {7 const columns = useMemo<MRT_ColumnDef<Person>[]>(8 //column definitions...33 );3435 return (36 <MaterialReactTable37 columns={columns}38 data={data}39 displayColumnDefOptions={{40 'mrt-row-actions': {41 size: 350, //set custom width42 muiTableHeadCellProps: {43 align: 'center', //change head cell props44 },45 },46 'mrt-row-numbers': {47 enableColumnOrdering: true, //turn on some features that are usually off48 enableResizing: true,49 muiTableHeadCellProps: {50 sx: {51 fontSize: '1.2rem',52 },53 },54 },55 'mrt-row-select': {56 enableColumnActions: true,57 enableHiding: true,58 size: 100,59 },60 }}61 enableColumnResizing62 enableColumnOrdering63 enableRowNumbers64 enableRowSelection65 enableRowActions66 renderRowActions={({ row }) => (67 <Box sx={{ display: 'flex', gap: '1rem' }}>68 <Button>Button 1</Button>69 <Button>Button 2</Button>70 <Button>Button 3</Button>71 </Box>72 )}73 />74 );75};7677export default Example;78
Create your own Display Columns
You do not have to use the built in Row Actions feature. You can just create your own display columns instead. It is as easy as creating a normal column, only specifying the columnDefType
as display
.
const columns = [{id: 'sendEmail',header: 'Send Email',columnDefType: 'display', //turns off data column features like sorting, filtering, etc.enableColumnOrdering: true, //but you can turn back any of those features on if you want like thisCell: ({ row }) => (<Button onClick={() => sendEmail(row.original.userId)}>Send Email</Button>),},{id: 'name',header: 'Name',accessorKey: 'name',},];