Lattice Grid

demo D09

Pinned and Frozen Columns, Left and Right in Angular

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 Angular, pinning a column is one key on a column definition: layout.pin set to 'start' or 'end'. The standalone LatticeGridComponent from @toclocoinc/lattice-grid/angular takes the whole configuration through its config input, so Account holds the leading edge and Monthly cost the trailing edge while the centre columns scroll under them. The grid above runs ten thousand cloud cost rows. The Angular code underneath is the same configuration, with the npm install and import lines above it and the notes after it on outputs and reading the pin back.

Install and import

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

import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { LatticeGridComponent, provideLattice } from '@toclocoinc/lattice-grid/angular';

The configuration

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { LatticeGridComponent, provideLattice } from '@toclocoinc/lattice-grid/angular';

// 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 */];



@Component({
  selector: 'app-root',
  standalone: true,
  imports: [LatticeGridComponent],
  template: '<lattice-grid [config]="grid" style="display:block;height:540px"></lattice-grid>',
})
export class AppComponent {
  grid = {
    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,
  };
}

bootstrapApplication(AppComponent, {
  providers: [provideLattice({ createGrid })],
});

API notes for pinned columns in Angular

  • 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.
  • Inputs. [config] takes every configuration key, including columns. [sort], [filters], [quickFilter] and [selectedKeys] are applied through the matching grid API. Give the <lattice-grid> element a height, as the demo’s height:540px does.
  • Outputs. Every grid event is an output under its kebab-case name, so column:pinned arrives as (column-pinned). The event carries id and side for one column (side is null when it was unpinned), or ids and pins, a map of id to side, for a batch call. (grid-ready) hands you the grid instance.
  • Reading the pin back. Take the instance from a viewChild on the component, or from (grid-ready). It is the same object createGrid returns, so grid.columns.all().filter((c) => c.layout.pin) lists the pinned columns and 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.
  • The gotcha. Inputs are compared by identity (Object.is) and never rebuild the grid. An inline [config]="{ columns: columns }" is a new object on every change detection pass, so hold the configuration in a field or a signal, as the code above does with grid.

How do I read which columns are pinned in Angular?

Declare grid = viewChild<LatticeGridComponent>('grid') with a template reference #grid on the element, or use the (grid-ready) output. The grid paints on its own schedule rather than Angular’s, so read it from a grid event or an event handler, not in ngAfterViewInit. Then filter columns.all() on layout.pin.

Does a column stay pinned when the config input changes?

A changed input is pushed into the grid already on screen and does not rebuild it, so scroll position, selection and any open editor stay as they were. The pinned columns are whatever the new columns declare in layout.pin. A pin the reader set at run time through columns.pin() lives in the grid, so write it back into your definitions from (column-pinned) if you replace the columns.

Can I use pinned columns with OnPush or zoneless change detection?

Yes. The component owns one element, builds the grid after render and pushes changed inputs in from ngOnChanges, so a host on OnPush still gets a live grid. The grid is created outside the zone, and an output you have bound, such as (column-pinned), re-enters it so your view repaints.

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.