Quick Start
Follow these steps to go from an empty container to an interactive grid with sorting, row selection, events, and state persistence.
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: 98, pinned: 'start', isSortable: true, }, { key: 'customer', name: 'Customer', width: 175, minWidth: 130, flex: 1, isSortable: true, isFilterable: true, }, { key: 'region', name: 'Region', width: 105, isSortable: true, isFilterable: true, }, { key: 'amount', name: 'Amount', width: 115, cellDataType: 'number', isSortable: true, formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, { key: 'status', name: 'Status', width: 105, isSortable: true, }, ];
const grid = new Grid<OrderRow>({ container: stage, ariaLabel: 'Quick start orders', columnDefs, rowData: createOrders(40), getRowId: (row) => row.id, rowSelection: { mode: 'multiple', showCheckbox: true, }, }); const stopTheme = syncGridTheme(stage);
const updateMeta = () => { const sel = grid.getSelectionState(); const selected = 'rowIds' in sel ? sel.rowIds.length : 0; const sort = grid .getSortModel() .map((s) => `${s.colId}:${s.direction}`) .join(', ') || 'none'; meta.textContent = `Selected: ${selected} · Sort: ${sort}`; };
grid.addEventListener('selectionChanged', updateMeta); grid.addEventListener('sortChanged', updateMeta);
let savedJson: string | null = null; const restoreBtn = addButton( controls, 'Restore state', () => { if (savedJson) grid.applyState(JSON.parse(savedJson)); }, { disabled: true }, ); addButton(controls, 'Save state', () => { savedJson = JSON.stringify(grid.getState()); restoreBtn.disabled = false; }); addButton(controls, 'Sort amount ↓', () => { grid.setSortModel([ { colId: 'amount', direction: 'desc', }, ]); });
updateMeta();
return () => { grid.destroy(); stopTheme(); };}1. Prepare a Sized Container
Section titled “1. Prepare a Sized Container”Give the host element an explicit height (or flex/grid constrained height) so NextGrid can measure the viewport and allocate row pools:
<div id="orders-grid" style="width: 100%; height: 360px;"></div>2. Define Columns and Row Data
Section titled “2. Define Columns and Row Data”Import Grid and the base stylesheet, declare a typed row interface, and define columnDefs:
import { Grid, type ColumnDef } from 'next-grid-js';import 'next-grid-js/styles.css';
interface Order { id: string; customer: string; region: string; amount: number; status: string;}
const columnDefs: readonly ColumnDef<Order>[] = [ { key: 'id', name: 'ID', width: 98, pinned: 'start', isSortable: true }, { key: 'customer', name: 'Customer', width: 175, flex: 1, isSortable: true }, { key: 'region', name: 'Region', width: 105, isSortable: true }, { key: 'amount', name: 'Amount', width: 115, cellDataType: 'number', isSortable: true, formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, { key: 'status', name: 'Status', width: 105, isSortable: true },];3. Add Row Identity & Selection
Section titled “3. Add Row Identity & Selection”Pass getRowId so selection, focus, and state survive sorting, filtering, and row transactions:
const grid = new Grid<Order>({ container: document.getElementById('orders-grid')!, ariaLabel: 'Quick start orders', columnDefs, rowData: orders, getRowId: (row) => row.id, rowSelection: { mode: 'multiple', showCheckbox: true },});4. Subscribe to Events & Persist State
Section titled “4. Subscribe to Events & Persist State”Listen to typed events via addEventListener and save or restore full snapshots via getState() / applyState():
grid.addEventListener('sortChanged', (event) => { console.log('Sort updated:', event.sort);});
const saved = JSON.stringify(grid.getState());localStorage.setItem('orders-grid', saved);
// Later, on an equivalent grid instance:grid.applyState(JSON.parse(saved));