Viewport Row Model
Use rowModelType: 'viewport' when your backend or WebSocket stream pushes live row updates directly to the currently visible row window.
import { Grid, type ColumnDef } from 'next-grid-js';import { createFakeViewportDatasource } from './data/fake-server';import type { OrderRow } from './data/orders';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';
export function mount({ stage, meta }: DemoMountContext): () => void { const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 96, pinned: 'start', }, { key: 'customer', name: 'Customer', width: 175, flex: 1, }, { key: 'quantity', name: 'Live Qty', width: 95, cellDataType: 'number', }, { key: 'amount', name: 'Amount', width: 120, cellDataType: 'number', formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, { key: 'status', name: 'Status', width: 105, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowModelType: 'viewport', viewportDatasource: createFakeViewportDatasource(120), viewportOptions: { initialRowCount: { kind: 'final', count: 120, }, }, getRowId: (row) => row.id, }); const stopTheme = syncGridTheme(stage);
grid.addEventListener('datasourceChanged', ({ data }) => { if (data.rowModelType === 'viewport') { meta.textContent = `Window ${data.firstRow}..${data.lastRow} · Connected: ${data.connected}`; } });
return () => { grid.destroy(); stopTheme(); };}ViewportDatasource Contract
Section titled “ViewportDatasource Contract”Implement connect(consumer):
const viewportDatasource: ViewportDatasource<OrderRow> = { connect(consumer) { // Call consumer.push({ generation, version, startRow, rows, rowCount }) // or consumer.error(generation, message) return { setViewport(request) { // request: { firstRow, lastRow, generation, sort, filters, quickFilter, context } }, destroy() { // Unsubscribe sockets/timers when the grid is destroyed or datasource replaced }, }; },};- High-frequency
consumer.push(...)updates coalesce cleanly into the grid’s single frame pipeline without tearing scroll position. - Replace the live datasource at runtime with
grid.setViewportDatasource(next).