Client Data & Transactions
Client-side grids store rows from rowData. Use setRowData to replace the entire dataset, or applyRowTransaction / applyRowTransactionAsync to insert, update, and remove rows incrementally while preserving scroll, selection, and cell renderers on untouched rows.
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 { let seq = 2000; const initial = createOrders(10); const rowsMap = new Map(initial.map((r) => [r.id, r]));
const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 96, isSortable: true, }, { key: 'customer', name: 'Customer', width: 175, flex: 1, isSortable: true, }, { key: 'quantity', name: 'Qty', width: 78, cellDataType: 'number', 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, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowData: initial, getRowId: (row) => row.id, }); const stopTheme = syncGridTheme(stage);
let lastStatus = 'Ready'; const syncMeta = (rowCount = initial.length) => { meta.textContent = `Rows: ${rowCount} · ${lastStatus}`; }; grid.addEventListener('modelUpdated', (e) => syncMeta(e.rowCount));
addButton(controls, 'Add row', () => { seq += 1; const row: OrderRow = { id: `ORD-${seq}`, customer: 'Helios Energy', region: 'EU-West', product: 'NVMe Edge Node', quantity: 3, price: 420, amount: 1260, status: 'Pending', orderedAt: '2026-12-04', }; rowsMap.set(row.id, row); const res = grid.applyRowTransaction({ addIndex: 0, add: [row], }); lastStatus = `Added (${res.dataEpoch.slice(0, 8)})`; syncMeta(); });
addButton(controls, 'Update first', () => { const addr = grid.getRowAddress(0); if (!addr || !('rowId' in addr) || !addr.rowId) return; const current = rowsMap.get(addr.rowId); if (!current) return; const updated: OrderRow = { ...current, quantity: current.quantity + 1, amount: (current.quantity + 1) * current.price, status: 'Shipped', }; rowsMap.set(addr.rowId, updated); grid.applyRowTransaction({ update: [ { rowId: addr.rowId, data: updated, }, ], }); lastStatus = `Updated ${addr.rowId}`; syncMeta(); });
addButton(controls, 'Remove first', () => { const addr = grid.getRowAddress(0); if (!addr || !('rowId' in addr) || !addr.rowId) return; rowsMap.delete(addr.rowId); grid.applyRowTransaction({ remove: [addr.rowId] }); lastStatus = `Removed ${addr.rowId}`; syncMeta(); });
addButton(controls, 'Async batch (3)', async () => { for (let i = 0; i < 3; i++) { seq += 1; const row: OrderRow = { id: `ORD-${seq}`, customer: 'Cobalt Systems', region: 'US-East', product: 'ARM64 Gateway Module', quantity: 2, price: 310, amount: 620, status: 'Paid', orderedAt: '2026-12-04', }; rowsMap.set(row.id, row); void grid.applyRowTransactionAsync({ add: [row] }); } const batch = await grid.flushAsyncTransactions(); lastStatus = batch ? `Async ${batch.status}` : 'Empty batch'; syncMeta(); });
syncMeta();
return () => { grid.destroy(); stopTheme(); };}Replacing Data (setRowData)
Section titled “Replacing Data (setRowData)”grid.setRowData(nextRows) replaces all client rows, advances dataEpoch, clears undo history, and recreates cell renderers. Use it when loading a completely new dataset.
Incremental Transactions (applyRowTransaction)
Section titled “Incremental Transactions (applyRowTransaction)”const result = grid.applyRowTransaction({ addIndex: 0, add: [{ id: 'ORD-2001', customer: 'Helios', /* ... */ }], update: [{ rowId: 'ORD-1001', data: updatedOrder }], remove: ['ORD-1005'],});- Requirements: Row identity (
getRowId,rowKey, orgenerateRowId) must be configured. - Order of operations:
removeruns first, thenupdate, thenadd(inserted ataddIndexafter removals, or appended if omitted). - Return value (
RowTransactionResult):{ status: 'applied' | 'unchanged', previousDataEpoch, dataEpoch, added, updated, removed }. - Events: Emits
rowDataChangedandmodelUpdated.
Batched Async Transactions (applyRowTransactionAsync)
Section titled “Batched Async Transactions (applyRowTransactionAsync)”For streaming updates, applyRowTransactionAsync(tx) queues changes into the next animation frame batch and returns a promise resolving to AsyncRowTransactionResult:
- Resolves with
{ status: 'applied' | 'unchanged', batchId, transaction }, or{ status: 'cancelled', batchId, reason }ifsetRowData('dataEpochChanged') ordestroy()('destroy') occurs before the batch flushes. - Call
await grid.flushAsyncTransactions()to commit queued batches immediately.