Column Pinning Example
Material React Table has an easy-to-enable column pinning feature. Columns can be pinned to the left or right of the table, and the column will be frozen in place while the rest of the table scrolls horizontally. See the Column Pinning Feature Guide for more information.
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
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