Skip to content

Pinning & Ordering

Pin key columns to the logical 'start' or 'end' edges so they stay visible during horizontal scrolling, and reorder columns interactively or via API.

Pinning & Nested Column Groups
import { Grid, type ColumnDef } from 'next-grid-js';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
import { addButton } from './ui';
interface ProductRow {
id: number;
name: string;
category: string;
warehouse: string;
quantity: number;
price: number;
}
const PRODUCT_ROWS: readonly ProductRow[] = [
{
id: 101,
name: 'NVMe Edge Node',
category: 'Compute',
warehouse: 'FRA-01',
quantity: 14,
price: 420,
},
{
id: 102,
name: '10GbE SFP+ Transceiver',
category: 'Network',
warehouse: 'IAD-02',
quantity: 48,
price: 65,
},
{
id: 103,
name: 'Rack PDU 24-Port',
category: 'Power',
warehouse: 'AMS-03',
quantity: 9,
price: 290,
},
{
id: 104,
name: 'Thermal Sensor Array',
category: 'Telemetry',
warehouse: 'SIN-01',
quantity: 32,
price: 85,
},
{
id: 105,
name: 'ARM64 Gateway Module',
category: 'Compute',
warehouse: 'FRA-01',
quantity: 18,
price: 310,
},
{
id: 106,
name: 'Patch Panel Cat6A',
category: 'Network',
warehouse: 'IAD-02',
quantity: 25,
price: 115,
},
{
id: 107,
name: 'UPS Battery Pack',
category: 'Power',
warehouse: 'AMS-03',
quantity: 6,
price: 540,
},
{
id: 108,
name: 'ECC DDR5 64GB Kit',
category: 'Memory',
warehouse: 'FRA-01',
quantity: 22,
price: 245,
},
{
id: 109,
name: 'DIN Rail Switch 8P',
category: 'Network',
warehouse: 'SIN-01',
quantity: 16,
price: 185,
},
{
id: 110,
name: 'KVM Console Tray',
category: 'Access',
warehouse: 'IAD-02',
quantity: 4,
price: 690,
},
];
export function mount({
stage,
controls,
meta,
}: DemoMountContext): () => void {
const columnDefs: readonly ColumnDef<ProductRow>[] = [
{
key: 'id',
name: 'ID',
width: 64,
minWidth: 54,
pinned: 'start',
isMovable: false,
},
{
key: 'name',
name: 'Product',
width: 220,
minWidth: 110,
},
{
key: 'category',
name: 'Category',
width: 135,
minWidth: 78,
},
{
key: 'warehouse',
name: 'Site',
width: 115,
minWidth: 70,
},
{
key: 'quantity',
name: 'Qty',
width: 95,
minWidth: 52,
cellDataType: 'number',
},
{
key: 'price',
name: 'Unit Price',
width: 135,
minWidth: 78,
cellDataType: 'number',
formatter: ({ value }) => `$${Number(value).toFixed(2)}`,
},
{
colId: 'total',
name: 'Total',
width: 96,
minWidth: 82,
pinned: 'end',
cellDataType: 'number',
valueGetter: ({ data }) => data.quantity * data.price,
formatter: ({ value }) => `$${Number(value).toFixed(2)}`,
},
];
const grid = new Grid<ProductRow>({
container: stage,
minCenterWidth: 100,
columnDefs,
columnGroups: [
{
groupId: 'catalog',
name: 'Catalog',
children: ['name', 'category', 'warehouse'],
},
{
groupId: 'inventory',
name: 'Inventory',
children: [
'quantity',
{
groupId: 'valuation',
name: 'Valuation',
children: ['price', 'total'],
},
],
},
],
rowData: [...PRODUCT_ROWS],
getRowId: (row) => String(row.id),
});
const stopTheme = syncGridTheme(stage);
const initialStateJson = JSON.stringify(grid.getColumnState());
addButton(controls, 'Fit columns', () => grid.sizeColumnsToFit());
addButton(controls, 'Autosize', () => {
const visibleIds = grid
.getColumnState()
.columns.filter((c) => c.isVisible)
.map((c) => c.colId);
grid.autoSizeColumns(visibleIds);
});
const pinBtn = addButton(controls, 'Unpin edges', () => {
const cols = grid.getColumnState().columns;
const hasPins = cols.some((c) => c.isVisible && c.pinned !== null);
if (hasPins) {
for (const c of cols) {
if (c.pinned !== null) grid.setColumnPinned(c.colId, null);
}
} else {
grid.setColumnPinned('id', 'start');
grid.setColumnPinned('total', 'end');
}
});
const resetBtn = addButton(
controls,
'Reset',
() => grid.resetColumnState(),
{ disabled: true },
);
const syncToolbar = () => {
const state = grid.getColumnState();
const visible = state.columns.filter((c) => c.isVisible);
const pinnedCount = visible.filter((c) => c.pinned !== null).length;
const centerWidth = Math.round(
visible
.filter((c) => c.pinned === null)
.reduce((sum, c) => sum + c.width, 0),
);
pinBtn.textContent = pinnedCount > 0 ? 'Unpin edges' : 'Pin edges';
resetBtn.disabled = JSON.stringify(state) === initialStateJson;
meta.textContent = `Pinned: ${pinnedCount} · Center: ${centerWidth}px`;
};
grid.addEventListener('columnStateChanged', syncToolbar);
syncToolbar();
return () => {
grid.destroy();
stopTheme();
};
}
  • Logical Edges: 'start' pins to the left in LTR and to the right in RTL; 'end' pins to the opposite edge.
  • Programmatic Pinning: Call grid.setColumnPinned(colId, 'start' | 'end' | null).
  • Center Width Guard: If pinning or widening a pinned column would reduce the center viewport below minCenterWidth, the command throws and keeps the prior state. If the browser window shrinks externally below minCenterWidth, the footer exposes an Unpin all columns recovery button.
  • Drag Reorder: Drag any column header with isMovable: true (the default) to reorder it within its pin zone (start, center, or end).
  • Programmatic Move: Call grid.moveColumn(colId, targetIndex). Columns cannot be dragged across different pin zones without changing pinned.