Skip to content

Align two or more grids ​

Since 1.6.0

alignGrids, api.alignWith and the gridsAligned and gridsUnaligned events are available since 1.6.0.

Sometimes you want a totals row under a list of orders, or a header-only grid above the body. Link two to four grids with alignGrids and their horizontal scrolling follows each other, and a column width, order or visibility changed in one grid is applied to the others.

ts
import { alignGrids } from 'maxgrid';

const detail = createGrid(detailEl, detailOptions);
const summary = createGrid(summaryEl, { ...summaryOptions, showHeader: false });

const link = alignGrids([detail, summary]); // default: horizontal scroll + width, order, visibility
// With just two grids you can also write:
// const link = detail.alignWith(summary);

link.unlink(); // remove the link

Options ​

ts
alignGrids(grids, {
  scroll: 'x', // 'x' horizontal (default) · 'y' vertical · 'both' · 'none'
  columns: ['width', 'order', 'visibility'], // default. Add 'pinned' to align pinning too
  master: 0, // index of the grid whose column state wins at the start (default 0)
});
OptionDefaultDescription
scroll'x'Which scroll direction to align. With 'none' only the columns are aligned.
columns['width', 'order', 'visibility']Which column properties to align. An empty array aligns only the scroll.
master0The column state of this grid is applied to the others when you link them.

It returns { unlink(), grids }. If you pass fewer than 2 or more than 4 grids, the same grid twice, or a bad option, it fails with invalid-option and nothing is linked.

Rules ​

  • Scroll is aligned by the distance from the start edge (getScroll().left), not by the raw position, so a grid with direction: 'rtl' follows with the same distance. It is applied once per frame, and the scroll notice caused by the alignment itself is not echoed back. A grid with a shorter scroll range stops at its own end.
  • Columns are aligned only for ids that exist in both grids; columns present in one grid are left alone. Dragging a width, moveColumn, setColumnVisible and pinColumn all propagate. Order aligns the relative order of the columns the grids share.
  • A value that is already the same is skipped, so the grids never ping-pong. If the receiving grid refuses a change (for example a locked column), only that grid gets an error event (where: 'alignGrids').
  • Columns hidden by width (responsive columns) are not mirrored; only what the user hid follows.
  • Collapsed header groups fold per grid. You can link a grid with groups to one without; widths and order still align.
  • When a linked grid is destroyed it drops out, and the link ends when fewer than two grids remain. unlink() and destroy() remove every listener and the pending frame.

Events ​

EventContent
gridsAlignedOnce per grid when linked: { count, index, master }.
gridsUnalignedOnce per grid when unlinked: { count, index, reason: 'unlink' | 'destroyed' }.

Examples ​