Column Ordering (DnD) Feature Guide
Whether you just want to change the default column order in your table or let columns be reordered by dragging and dropping, Material React Table has a simple API for this.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| MRT Column Ordering DnD Docs | ||
2 |
| MRT Column Ordering DnD Docs | |||
3 |
| TanStack Table Column Ordering Docs | |||
4 |
| ||||
5 |
| ||||
Relevant Column Options
Relevant State
# | State Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| TanStack Table Column Ordering Docs | ||
2 |
| ||||
3 |
| ||||
Change the Default Column Order
By Default, Material React Table will order the columns in the order they are defined in the columns
table option. And Display Columns such as Actions, Selection, Expansion, etc., get added to either the beginning or the end of the table. You can customize all of this by defining your own columnOrder
State and passing it either to the initialState
or state
props.
The Column Order State is an array of string column ids, that come from the ids or accessorKeys that you defined in your column definitions.
const table = useMaterialReactTable({data,columns,enableRowSelection: true,initialState: {columnOrder: ['name','email','phone','city','country','mrt-row-select', //move the built-in selection column to the end of the table],},});return <MaterialReactTable table={table} />;
Manage Column Order State
If you need to manage the columnOrder
state yourself, you can do so in the state
table option and onColumnOrderChange
callback, but you will also need to initialize the columnOrder
state yourself.
const columns = [//...];//easy shortcut to initialize the columnOrder state as array of column idsconst [columnOrder, setColumnOrder] = useState(columns.map((c) => c.accessorKey), //array of column ids);const table = useMaterialReactTable({data,columns,enableRowSelection: true,state: {columnOrder,},onColumnOrderChange: setColumnOrder,});return <MaterialReactTable table={table} />;
Enable Column Ordering with Drag and Drop
Material React Table has a built-in drag and drop feature to reorder columns. This feature is enabled by passing the enableColumnOrdering
table option.
The ability for a column to have a drag and drop handle can be specified by setting the enableColumnOrdering
option on the column.
First Name | Last Name | Address | City | State |
---|---|---|---|---|
Dylan | Murray | 261 Erdman Ford | East Daphne | Kentucky |
Raquel | Kohler | 769 Dominic Grove | Columbus | Ohio |
Ervin | Reinger | 566 Brakus Inlet | South Linda | West Virginia |
Brittany | McCullough | 722 Emie Stream | Lincoln | Nebraska |
Branson | Frami | 32188 Larkin Turnpike | Charleston | South Carolina |
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',15 },16 //column definitions...30 {31 accessorKey: 'state',32 enableColumnOrdering: false, //disable column ordering for this column,33 header: 'State',34 },35 ],36 [],37 );3839 const table = useMaterialReactTable({40 columns,41 data,42 enableColumnOrdering: true,43 });4445 return <MaterialReactTable table={table} />;46};4748export default Example;49
View Extra Storybook Examples