Column Resizing Feature Guide
Material React Table has a built-in column resizing draggable handle feature.
The Column Size features was recently split into its own Guide. View that guide as a prerequisite to this one.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| MRT Column Resizing Docs | ||
2 |
|
| MRT Column Resizing Docs | ||
3 |
| TanStack Table Core Table Docs | |||
4 |
| MRT Display Columns Docs | |||
5 |
| MRT Display Columns Docs | |||
6 |
| MRT Column Resizing Docs | |||
7 |
|
| TODO | ||
8 |
| TanStack Table Column Sizing Docs | |||
9 |
| TanStack Table Column Sizing Docs | |||
Relevant Column Options
Relevant State
# | State Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| TanStack Table Column Sizing Docs | ||
2 |
|
| TanStack Table Column Sizing Docs | ||
Initial Column Sizes
Column sizes will behave differently depending on which layoutMode
you have set.
See the Column Size Guide for more information on layout modes and how to set initial column sizes properly for you use case.
Enable Column Resizing Feature
enableColumnResizing
is the boolean table option that enables the column resizing feature.
const table = useMaterialReactTable({columns,data,enableColumnResizing: true,});return <MaterialReactTable table={table} />;
You can disable specific columns from being resizable by setting the enableResizing
column option to false in their respective column definition.
Column Resize Mode
The default columnResizeMode
is onChange
(in MRT versions v1.7+), which means that the column resizing will occur immediately as the user drags the column resize handle. If you are running into performance issues because of many other enabled features, you might want to set the columnResizeMode
to onEnd
instead. This will make the column resizing only occur after the user has finished dragging the column resize handle and released their mouse.
const table = useMaterialReactTable({columns,data,enableColumnResizing: true,columnResizeMode: 'onEnd', //instead of the default "onChange" mode});
First Name | Last Name | Email Address | City | Country | |
---|---|---|---|---|---|
Dylan | Murray | dmurray@yopmail.com | East Daphne | USA | |
Raquel | Kohler | rkholer33@yopmail.com | Columbus | USA | |
Ervin | Reinger | ereinger@mailinator.com | Toronto | Canada | |
Brittany | McCullough | bmccullough44@mailinator.com | Lincoln | USA | |
Branson | Frami | bframi@yopmain.com | New York | USA |
1import { useMemo } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';89const Example = () => {10 const columns = useMemo<MRT_ColumnDef<Person>[]>(11 () => [12 {13 accessorKey: 'firstName',14 header: 'First Name', //uses the default width from defaultColumn prop15 },16 {17 accessorKey: 'lastName',18 header: 'Last Name',19 },20 {21 accessorKey: 'email',22 header: 'Email Address',23 size: 250, //increase the width of this column24 },25 {26 accessorKey: 'city',27 header: 'City',28 size: 200, //decrease the width of this column29 enableResizing: false, //disable resizing for this column30 },31 {32 accessorKey: 'country',33 header: 'Country',34 size: 140, //decrease the width of this column35 },36 ],37 [],38 );3940 const table = useMaterialReactTable({41 columns,42 data,43 //optionally override the default column widths44 defaultColumn: {45 maxSize: 400,46 minSize: 80,47 size: 160, //default size is usually 18048 },49 enableColumnResizing: true,50 columnResizeMode: 'onChange', //default51 });5253 return <MaterialReactTable table={table} />;54};5556export default Example;57
Column Growing
MRT V2 has a new "opposite" behavior in regards to column sizes when column resizing is enabled compared to MRT V2
When column resizing is enabled, by default, a layoutMode of "grid-no-grow"
will be applied internally. This means that columns will have an absolute size and they will NOT grow to fill in the remaining space of the table. You can let columns grow to fill in the remaining space by changing the layoutMode
back to "grid"
or "semantic"
.
const table = useMaterialReactTable({columns,data,enableColumnResizing: true,layoutMode: 'grid', //instead of the default "grid-no-grow" when column resizing is enabled});
Alternatively, if you only want certain columns to grow to fill the remaining space, you can set the grow
column option to true
in their respective column definitions.
const columns = [//...{accessorKey: 'address',header: 'Address',size: 250,grow: true, //allow this column to grow to fill the remaining space},];
This is discussed in more detail in the Column Size Guide.
Column Resize Direction
New in V2.1
If you are displaying your table in a RTL (right-to-left) language, you can set the columnResizeDirection
table option to "rtl"
to make the column resize handle appear on the left side of the column instead of the right side. This may behave differently depending on which Emotion or MUI theme settings you have enabled.
If you have already set the proper theme.direction
setting in your MUI theme, then this option will already have been set automatically for you, but you can still override it using the columnResizeDirection
table option.
const table = useMaterialReactTable({columns,data,enableColumnResizing: true,columnResizeDirection: 'rtl', //instead of the default "ltr" direction});return (<div style={{ direction: 'rtl' }}>{' '}{/* app-wide style? */}<MaterialReactTable table={table} /></div>);
عملیات | انتخاب | نام | نام خانوادگی | سن | |
---|---|---|---|---|---|
میسی | 26 | ||||
پریا | 28 | ||||
علی | 33 |
1//Import Material React Table and its Types2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';34//Import Material React Table Translations5import { MRT_Localization_FA } from 'material-react-table/locales/fa';67//mock data8import { data, type Person } from './makeData';910const columns: MRT_ColumnDef<Person>[] = [11 //column definitions...26];2728const Example = () => {29 return (30 <MaterialReactTable31 columns={columns}32 data={data}33 defaultColumn={{ size: 250 }}34 columnResizeDirection="rtl"35 enableColumnFilterModes36 enableColumnOrdering37 enableColumnResizing38 enableEditing39 enableColumnPinning40 enableRowActions41 enableRowSelection42 enableSelectAll={false}43 initialState={{ showColumnFilters: true, showGlobalFilter: true }}44 localization={MRT_Localization_FA}45 />46 );47};4849//App.tsx or similar50import { createTheme, ThemeProvider, useTheme } from '@mui/material';51import { faIR } from '@mui/material/locale';5253const ExampleWithThemeProvider = () => {54 const theme = useTheme(); //replace with your theme/createTheme5556 return (57 //Setting Material UI locale as best practice to result in better accessibility58 <ThemeProvider theme={createTheme({ ...theme, direction: 'rtl' }, faIR)}>59 <div style={{ direction: 'rtl' }}>60 <Example />61 </div>62 </ThemeProvider>63 );64};6566export default ExampleWithThemeProvider;67
Enable Resizing on Built-in Display Columns
As discussed further in the Display Columns Guide, you can customize the options of the built-in columns that get generated under the hood by MRT by enabling certain features.
Here, we can enable column resizing on the built-in row numbers column by setting the enableResizing
column option to true in the displayColumnDefOptions
table option.
const table = useMaterialReactTable({columns,data,displayColumnDefOptions: {'mrt-row-numbers': {enableResizing: true, //allow the row numbers column to be resizedsize: 40,grow: false, //new in v2.8 - do not fill remaining space using this column},},enableRowNumbers: true,layoutMode: 'grid', // `grow` only works in the grid* layout modes});return <MaterialReactTable table={table} />;
View Extra Storybook Examples