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 Props
# | Prop Name | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 |
| ||||
2 |
| TanStack Table Column Pinning Docs | |||
Relevant Column Options
# | Column Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
No records to display |
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
prop to true
.
<MaterialReactTable data={data} columns={columns} enableColumnPinning />
Pin (Freeze) Columns By Default
Columns can start out pinned in your table by setting the columnPinning
states in initialState
or state
.
<MaterialReactTabledata={data}columns={columns}enableColumnPinninginitialState={{ columnPinning: { left: ['email'] } }} //pin email column to left by default/>
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 { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';3import { data, type Person } from './makeData';4import { MenuItem } from '@mui/material';56const Example = () => {7 const columns = useMemo<MRT_ColumnDef<Person>[]>(8 () => [9 {10 accessorKey: 'id',11 enableColumnPinning: false, //disable column pinning for this column12 header: 'ID',13 size: 50,14 },15 {16 accessorKey: 'firstName',17 header: 'First Name',18 },19 {20 accessorKey: 'middleName',21 header: 'Middle Name',22 },23 {24 accessorKey: 'lastName',25 header: 'Last Name',26 },27 {28 accessorKey: 'address',29 header: 'Address',30 size: 300,31 },32 {33 accessorKey: 'city', //this column gets pinned to the right by default because of the initial state,34 header: 'City',35 },36 {37 accessorKey: 'state', //this column gets pinned left by default because of the the initial state,38 header: 'State',39 },40 {41 accessorKey: 'country',42 header: 'Country',43 },44 ],45 [],46 );4748 return (49 <MaterialReactTable50 columns={columns}51 data={data}52 enableColumnPinning53 enableRowActions54 renderRowActionMenuItems={() => [55 <MenuItem key="action">Action</MenuItem>,56 ]}57 initialState={{58 columnPinning: { left: ['mrt-row-actions', 'state'], right: ['city'] },59 }}60 />61 );62};6364export default Example;65
View Extra Storybook Examples