Lattice Grid

demo D09

Pinned and Frozen Columns, Left and Right in React

Left and right pinned regions scrolling independently

layout: { pin: 'start' | 'end' }

This grid holds a chosen set of columns fixed against the left and right edges while the rest scroll underneath them. It keeps the identifying fields, an id or a name, in view no matter how far across a wide table someone scrolls, so a row never loses its label.

Building…
Loading a live grid…

In React, pinning a column is one key on the column definition: layout.pin set to 'start' or 'end'. The LatticeGrid component you build with createLatticeGrid takes the columns array as a prop, so Account holds the left edge and Monthly cost the right while the centre columns scroll sideways under them. The grid above runs ten thousand cloud cost rows. The React code underneath is the same configuration, with the npm install and import lines above it, and the notes after it cover reading the pin back through the ref.

Install and import

npm install @toclocoinc/lattice-grid
# free on localhost; a production domain needs a licence key (see /pricing/)

import React from 'react';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { createLatticeGrid } from '@toclocoinc/lattice-grid/modules/react';

The configuration

<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.min.css">

<div id="grid"></div>

<script type="module">
  import React from 'https://esm.sh/react@18';
  import { createRoot } from 'https://esm.sh/react-dom@18/client';
  import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.esm.min.js';
  import createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/modules/react.esm.min.js';

  const LatticeGrid = createLatticeGrid({ React, createGrid });

  // pin: 'start' holds a column against the left edge; pin: 'end' against the
  // right. The pinned columns stay put while the rest scroll under them.
  const columns = [
    { field: 'account', title: 'Account', filter: { type: 'set' }, layout: { pin: 'start', width: 200 } },
    { field: 'service', title: 'Service', filter: { type: 'set' } },
    { field: 'resource', title: 'Resource', layout: { flex: 1, min: 200, max: 320 } },
    { field: 'region', title: 'Region', filter: { type: 'set' } },
    { field: 'environment', title: 'Env', filter: { type: 'set' },
      cell: { decoration: 'pill', variant: { map: {
        prod: 'danger', 'prod-2': 'danger', 'definitely-prod': 'danger',
        staging: 'warning', untagged: 'warning', test: 'info',
        dev: 'success', 'not-prod': 'neutral',
      } } } },
    { field: 'change', title: 'Change', type: 'number',
      layout: { width: 140, min: 140 }, format: { style: 'percent', decimals: 1 } },
    { field: 'cost', title: 'Monthly cost', type: 'number', total: 'sum',
      layout: { width: 170, pin: 'end' },
      format: { style: 'currency', currency: 'USD', decimals: 2 } },
  ];

  const rows = [/* 10,000 cloud cost records */];

  function App() {
    return (
      <LatticeGrid
        rowKey="id"
        selection="multiple"
        toolPanel={{ side: 'left', panels: ['columns', 'filters', 'views', 'quick'],
          actions: ['undo', 'redo', 'export', 'restore', 'maximise'], exportName: 'lattice-demo' }}
        columns={columns}
        rows={rows}
        style={{ height: '540px' }}
      />
    );
  }

  createRoot(document.getElementById('grid')).render(<App />);
</script>

API notes for pinned columns in React

  • Which property pins. layout: { pin: 'start' } holds a column against the leading edge and pin: 'end' against the trailing edge. The sides are start and end rather than left and right, so a right-to-left layout needs no change. The demo pins account to the start and cost to the end. A pinned column keeps its edge while there is something to scroll; where the columns do not fill the grid, the spare width falls beyond the last column rather than in front of it.
  • Props. Every grid configuration key is a prop on LatticeGrid, so columns, rows, rowKey and selection go in as written in the code above. When the reference of a prop changes the component applies it to the live grid with grid.setAll() and never rebuilds the grid.
  • Callback. onColumnPinned receives the column:pinned event: id and side for one column (side is null when it was unpinned), or ids and pins, a map of id to side, when several were pinned in one batch call. The prop name follows the same rule as onCellChanged for cell:changed.
  • Reading the pin back. Take the grid from the ref, after mount: ref.current.grid.columns.all().filter((c) => c.layout.pin) lists the pinned columns with their side, and grid.columns.state() returns serialisable column state that includes each column’s pin.
  • Changing a pin at run time. grid.columns.pin('account', 'end') moves a column, and grid.columns.pin('account', null) returns it to the scrolling body. Both go on the undo timeline, and an array of ids with one side, or a map of id to side, is one history entry and one event.
  • The gotcha. A pin set through columns.pin() lives in the grid, not in your columns array. If you hand the component a new columns array, the definitions you pass are what it applies, so keep the reader’s pins by writing them back into your definitions from onColumnPinned instead of assuming they carry across. Define the array once, at module scope or in useMemo, so a re-render that does not change the columns does not re-apply them.

How do I read which columns are pinned from a React ref?

Hold the ref the adapter forwards, for example const ref = useRef(null), and pass it as ref={ref}. The ref exposes .grid once the component has mounted, and it is null before then, so read it inside an event handler or an effect rather than during render. Then ref.current.grid.columns.all().filter((c) => c.layout.pin) gives the pinned columns, and c.layout.pin is 'start' or 'end'.

Does a column stay pinned when my React component re-renders?

A re-render that passes the same columns reference changes nothing in the grid. A new reference is applied to the existing grid with setAll() and the grid is not rebuilt, so the pinned columns are whatever your new definitions declare in layout.pin. Keep the array stable with useMemo or a module constant.

How do I pin a column on a button click in React?

Call ref.current.grid.columns.pin('account', 'start') in the click handler. Pass null as the side to unpin it. The call is recorded on the undo timeline, and onColumnPinned fires with the new side, so your own state can follow it.

Pinning columns to keep key fields in view while the rest scrolls

Pinned columns, also called frozen columns, hold a chosen set of fields fixed against the left or right edge of a JavaScript data grid while the remaining columns scroll independently underneath them. Reach for this whenever a wide table carries an identifying column, such as a name or an order number, that a reader needs alongside whatever metric they have scrolled across to compare, or whenever row-level actions belong on the right edge regardless of horizontal scroll position. Lattice Grid controls this per column through layout: { pin: 'start' | 'end' }, so a column definition declares its own pinned region rather than the grid applying a single global split; a leading identifier column takes 'start' and a trailing actions column takes 'end', with the unpinned columns forming a centre region that scrolls on its own.

The pinned and centre regions render as separate scrolling containers sharing one header and one set of row boundaries, so vertical scrolling stays in sync across all three without a manual reconciliation step. Horizontal scroll position, sort state and column widths in the pinned regions are unaffected by scrolling the centre region, and keyboard navigation moves across the boundary between regions as a single continuous row rather than treating pinned columns as a separate tab stop sequence. Because only the centre region’s off-screen columns are virtualised out of the DOM, adding pinned columns does not change how many rows the grid can hold before scroll performance degrades.

How do you freeze columns in a JavaScript data grid?

Set layout: { pin: 'start' } on a column definition to fix it to the left edge, or pin: 'end' to fix it to the right edge. Lattice Grid renders pinned columns in their own scrolling region alongside the centre columns, keeping them in view while the rest of the row scrolls horizontally. See the step-by-step how to freeze columns guide for a runnable example and the source on GitHub.