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
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 enablePinning
prop to true
.
<MaterialReactTable data={data} columns={columns} enablePinning />
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}enablePinninginitialState={{ columnPinning: { left: ['email'] } }} //pin email column to left by default/>
Column Pinning Example
State | ID | First Name | Middle Name | Last Name | Address | City |
---|---|---|---|---|---|---|
Kentucky | 1 | Dylan | Sprouse | Murray | 261 Erdman Ford | East Daphne |
Ohio | 2 | Raquel | Hakeem | Kohler | 769 Dominic Grove | Columbus |
West Virginia | 3 | Ervin | Kris | Reinger | 566 Brakus Inlet | South Linda |
Nebraska | 4 | Brittany | Kathryn | McCullough | 722 Emie Stream | Lincoln |
South Carolina | 5 | Branson | John | Frami | 32188 Larkin Turnpike | Charleston |
1import React, { FC, useMemo } from 'react';2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';3import { data, Person } from './makeData';45const Example: FC = () => {6 const columns = useMemo<MRT_ColumnDef<Person>[]>(7 () => [8 {9 accessorKey: 'id',10 enablePinning: false, //disable column pinning for this column11 header: 'ID',12 size: 50,13 },14 {15 accessorKey: 'firstName',16 header: 'First Name',17 },18 {19 accessorKey: 'middleName',20 header: 'Middle Name',21 },22 {23 accessorKey: 'lastName',24 header: 'Last Name',25 },26 {27 accessorKey: 'address',28 header: 'Address',29 size: 300,30 },31 {32 accessorKey: 'city', //this column gets pinned to the right by default because of the initial state,33 header: 'City',34 },3536 {37 accessorKey: 'state', //this column gets pinned left by default because of the the initial state,38 header: 'State',39 },40 ],41 [],42 );4344 return (45 <MaterialReactTable46 columns={columns}47 data={data}48 enablePinning49 initialState={{ columnPinning: { left: ['state'], right: ['city'] } }}50 />51 );52};5354export default Example;55