Skip to content

Lazy RowSource

When your application generates rows mathematically, reads from typed arrays, or indexes a massive local buffer, RowSource<T> lets NextGrid query only the rows currently inside the viewport plus rowOverscan.

1,000,000 Lazy Rows
import {
Grid,
type ColumnDef,
type GridRenderDiagnostics,
} from 'next-grid-js';
import {
createMillionRowSource,
type TelemetryRow,
} from './data/telemetry';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
export function mount({ stage, meta }: DemoMountContext): () => void {
let lastFrameText = '';
const columnDefs: readonly ColumnDef<TelemetryRow>[] = [
{
key: 'id',
name: 'ID',
width: 118,
pinned: 'start',
isMovable: false,
},
{
key: 'region',
name: 'Region',
width: 120,
minWidth: 105,
flex: 1,
},
{
key: 'uptime',
name: 'Uptime',
width: 110,
minWidth: 95,
flex: 1,
cellDataType: 'number',
formatter: ({ value }) => `${Number(value).toFixed(2)}%`,
},
{
key: 'latency',
name: 'Latency',
width: 110,
minWidth: 95,
flex: 1,
cellDataType: 'number',
formatter: ({ value }) => `${Number(value).toFixed(1)} ms`,
},
{
key: 'volume',
name: 'Volume',
width: 135,
minWidth: 115,
flex: 1.15,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
{
key: 'status',
name: 'Status',
width: 115,
minWidth: 95,
flex: 1,
},
];
const grid = new Grid<TelemetryRow>({
container: stage,
columnDefs,
rowSource: createMillionRowSource(1_000_000),
rowOverscan: 6,
columnOverscan: 2,
diagnostics: {
onFrame: (d: Readonly<GridRenderDiagnostics>) => {
const text = `Rows: 1,000,000 · DOM rows: ${d.dom.activeRows} · ${d.renderDurationMs.toFixed(1)} ms`;
if (text !== lastFrameText) {
lastFrameText = text;
meta.textContent = text;
}
},
},
});
const stopTheme = syncGridTheme(stage);
return () => {
grid.destroy();
stopTheme();
};
}
const source: RowSource<TelemetryRow> = {
getRowCount: () => 1_000_000,
getRow: (index) => (index >= 0 && index < 1_000_000 ? makeRow(index) : undefined),
getRowId: (row) => row.id,
// Optional fast reverse lookup:
getRowIndexById: (rowId) => parseIndex(rowId),
};
const grid = new Grid<TelemetryRow>({
container,
columnDefs,
rowSource: source,
});
  • Returning undefined: If getRow(index) returns undefined, the row renders as unavailable without crashing.
  • No Client Processing / Grouping / Pivot: rowSource does not run client-side sorting, filtering, grouping, or pivot; sort/filter your underlying source and call grid.setRowSource(nextSource) or grid.refreshRows().
  • Controlled Editing: Editing a rowSource cell emits cellEditRequest rather than mutating the source directly.