MRT logoMaterial React Table

On This Page

    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

    1
    boolean
    2
    OnChangeFn<ColumnPinningState>
    TanStack Table Column Pinning Docs

    Relevant Column Options

    No records to display

    Relevant State

    1
    { left: Array<string>, right: Array<string> }
    { left: [], right: [] }
    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.

    <MaterialReactTable
    data={data}
    columns={columns}
    enableColumnPinning
    initialState={{ columnPinning: { left: ['email'] } }} //pin email column to left by default
    />

    Column Pinning Example

    Demo

    Open StackblitzOpen Code SandboxOpen on GitHub
    Kentucky1DylanSprouseMurray261 Erdman FordUnited StatesEast Daphne
    Ohio2RaquelHakeemKohler769 Dominic GroveUnited StatesColumbus
    West Virginia3ErvinKrisReinger566 Brakus InletUnited StatesSouth Linda
    Nebraska4BrittanyKathrynMcCullough722 Emie StreamUnited StatesLincoln
    South Carolina5BransonJohnFrami32188 Larkin TurnpikeUnited StatesCharleston
    British Columbia6BrandonJoeKutch5660 Kuhn VillageCanadaVancouver

    1-6 of 6

    Source Code

    1import { useMemo } from 'react';
    2import { MaterialReactTable, type MRT_ColumnDef } from 'material-react-table';
    3import { data, type Person } from './makeData';
    4import { MenuItem } from '@mui/material';
    5
    6const Example = () => {
    7 const columns = useMemo<MRT_ColumnDef<Person>[]>(
    8 () => [
    9 {
    10 accessorKey: 'id',
    11 enableColumnPinning: false, //disable column pinning for this column
    12 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 );
    47
    48 return (
    49 <MaterialReactTable
    50 columns={columns}
    51 data={data}
    52 enableColumnPinning
    53 enableRowActions
    54 renderRowActionMenuItems={() => [
    55 <MenuItem key="action">Action</MenuItem>,
    56 ]}
    57 initialState={{
    58 columnPinning: { left: ['mrt-row-actions', 'state'], right: ['city'] },
    59 }}
    60 />
    61 );
    62};
    63
    64export default Example;
    65

    View Extra Storybook Examples