Column Sizing
NextGrid supports fixed pixel widths, proportional flex distribution, interactive pointer resizing, and programmatic sizing methods.
import { Grid, type ColumnDef } from 'next-grid-js';import { createOrders, type OrderRow } from './data/orders';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';import { addButton } from './ui';
export function mount({ stage, controls, meta,}: DemoMountContext): () => void { const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 95, minWidth: 75, maxWidth: 140, }, { key: 'customer', name: 'Customer', width: 180, minWidth: 120, flex: 1.5, }, { key: 'product', name: 'Product', width: 180, minWidth: 120, flex: 1, }, { key: 'region', name: 'Region', width: 110, minWidth: 85, }, { key: 'amount', name: 'Amount', width: 115, minWidth: 90, cellDataType: 'number', formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowData: createOrders(25), getRowId: (row) => row.id, }); const stopTheme = syncGridTheme(stage);
const updateMeta = () => { const total = grid .getColumnState() .columns.filter((c) => c.isVisible) .reduce((sum, c) => sum + Math.round(c.width), 0); meta.textContent = `Total width: ${total}px`; };
grid.addEventListener('columnStateChanged', updateMeta); addButton(controls, 'Fit to grid', () => grid.sizeColumnsToFit()); addButton(controls, 'Autosize all', () => { grid.autoSizeColumns( grid.getColumnState().columns.map((c) => c.colId), ); }); addButton(controls, 'Reset', () => grid.resetColumnState()); updateMeta();
return () => { grid.destroy(); stopTheme(); };}Sizing Options
Section titled “Sizing Options”| Property | Type | Default | Description |
|---|---|---|---|
width |
number |
150 |
Initial width in px |
minWidth |
number |
40 |
Lower width clamp during resize, flex, and autosize |
maxWidth |
number |
Infinity |
Upper width clamp |
flex |
number | null |
null |
Proportional share of remaining center viewport width |
isResizable |
boolean |
true |
Enables pointer drag-resize and double-click autosize on the header handle |
minCenterWidth |
number |
120 |
Minimum center scroll area width guarded against pinned column growth |
Interactive & Programmatic Sizing
Section titled “Interactive & Programmatic Sizing”- Pointer Resize & Double-Click: Drag the right edge handle of any resizable column header to resize interactively, or double-click the resize handle to autosize that column to its sampled cell contents.
sizeColumnsToFit(colIds?): Scales eligible center columns proportionally to fill the current center viewport width.autoSizeColumns(colIds): Measures rendered/sampled formatted cell text widths and header labels forcolIdsand clamps to[minWidth, maxWidth].- Container Resize: A
ResizeObserverrecalculatesflexcolumns automatically when the grid container resizes.