Column Pinning Feature Guide
Column pinning is a cool feature that lets users pin (freeze) columns to the left or right of the table. Pinned columns will not scroll horizontally with the rest of the columns so that they always stay visible to the user.
Relevant Table Options
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| ||||
2 |
| TanStack Table Column Pinning Docs | |||
Relevant Column Options
Relevant State
# | State Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
|
| TanStack Table Column Pinning Docs | ||
Enable Column Pinning
Column pinning can simply be enabled by setting the enableColumnPinning
table option to true
.
const table = useMaterialReactTable({data,columns,enableColumnPinning: true,});return <MaterialReactTable table={table} />;
Pin (Freeze) Columns By Default
Columns can start out pinned in your table by setting the columnPinning
states in initialState
or state
.
const table = useMaterialReactTable({data,columns,enableColumnPinning: true,initialState: { columnPinning: { left: ['email'] } }, //pin email column to left by default});return <MaterialReactTable table={table} />;
Apply Absolute Column Widths
You might consider using the layoutMode: 'grid-no-grow'
table option to give all columns an exact width if you don't want the columns collapsing a little while scrolling. Some people like this subtle behavior, but others do not.
const table = useMaterialReactTable({data,columns,enableColumnPinning: true,layoutMode: 'grid-no-grow',});return <MaterialReactTable table={table} />;
Column Pinning Example
Actions | State | ID | First Name | Middle Name | Last Name | Address | Country | City | |
---|---|---|---|---|---|---|---|---|---|
Kentucky | 1 | Dylan | Sprouse | Murray | 261 Erdman Ford | United States | East Daphne | ||
Ohio | 2 | Raquel | Hakeem | Kohler | 769 Dominic Grove | United States | Columbus | ||
West Virginia | 3 | Ervin | Kris | Reinger | 566 Brakus Inlet | United States | South Linda | ||
Nebraska | 4 | Brittany | Kathryn | McCullough | 722 Emie Stream | United States | Lincoln | ||
South Carolina | 5 | Branson | John | Frami | 32188 Larkin Turnpike | United States | Charleston | ||
British Columbia | 6 | Brandon | Joe | Kutch | 5660 Kuhn Village | Canada | Vancouver |
10
1-6 of 6
1import { useMemo } from 'react';2import {3 MaterialReactTable,4 useMaterialReactTable,5 type MRT_ColumnDef,6} from 'material-react-table';7import { data, type Person } from './makeData';8import { MenuItem } from '@mui/material';910const Example = () => {11 const columns = useMemo<MRT_ColumnDef<Person>[]>(12 () => [13 {14 accessorKey: 'id',15 enableColumnPinning: false, //disable column pinning for this column16 header: 'ID',17 size: 50,18 },19 //column definitions...38 {39 accessorKey: 'city', //this column gets pinned to the right by default because of the initial state,40 header: 'City',41 },42 {43 accessorKey: 'state', //this column gets pinned left by default because of the the initial state,44 header: 'State',45 },46 {47 accessorKey: 'country',48 header: 'Country',49 },50 ],51 [],52 );5354 const table = useMaterialReactTable({55 columns,56 data,57 enableColumnPinning: true,58 enableRowActions: true,59 layoutMode: 'grid-no-grow', //constant column widths60 renderRowActionMenuItems: () => [<MenuItem key="action">Action</MenuItem>],61 initialState: {62 columnPinning: { left: ['mrt-row-actions', 'state'], right: ['city'] },63 },64 });6566 return <MaterialReactTable table={table} />;67};6869export default Example;70
View Extra Storybook Examples