Skip to content

State Persistence

NextGrid exposes a JSON-serializable state snapshot (GridState, currently version 5) via getState() and restores full snapshots or partial patches via applyState(patch) and initialState.

Debounced Persistence (localStorage / Server)

Section titled “Debounced Persistence (localStorage / Server)”
let timer: ReturnType<typeof setTimeout> | undefined;
grid.addEventListener('stateChanged', (event) => {
if (event.source === 'stateRestore') return; // avoid redundant write on restore
clearTimeout(timer);
timer = setTimeout(() => {
localStorage.setItem('orders-grid-v5', JSON.stringify(event.state));
}, 200);
});
const saved = localStorage.getItem('orders-grid-v5');
if (saved) {
grid.applyState(JSON.parse(saved));
}
  • Serialized in v5: sort, filters, quickFilter, pagination, columns (ColumnState), hierarchy (HierarchyState), selection (SelectionState v2/v3), pivot, details, pinnedTotals, focus, dormantColumns, and suspended.
  • Excluded: Row payloads (rowData), mounted child grids, DOM nodes, remote caches, measured height caches, supplied pinnedRows, cellSpans, and callbacks (valueGetter, formatter, aggregateFunctions, detail.renderer). Reinstall matching callbacks and columnDefs before calling applyState.

applyState accepts partial v5 patches and legacy version: 1..4 patches. Omitted groups stay intact, and candidate states validate atomically before publication so invalid patches throw without mutating live state.