Aggregation
Use valueColumns together with rowGrouping or treeData to aggregate numeric or custom values per group and across the entire dataset.
import { Grid } from 'next-grid-js';import { createSales, type SaleRow } from './data/sales';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';import { addButton } from './ui';
export function mount({ stage, controls, meta,}: DemoMountContext): () => void { let scope: 'filtered' | 'all' = 'filtered';
const grid = new Grid<SaleRow>({ container: stage, columnDefs: [ { key: 'region', name: 'Region', width: 105, }, { key: 'product', name: 'Product', width: 110, isFilterable: true, }, { key: 'units', name: 'Units', width: 85, cellDataType: 'number', }, { key: 'revenue', name: 'Revenue', width: 125, cellDataType: 'number', formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, ], rowData: createSales(), getRowId: (row) => row.id, rowGrouping: { columns: ['region'], autoColumn: { name: 'Region', width: 170, pinned: 'start', }, }, valueColumns: [ { colId: 'units', aggFunc: 'avg', }, { colId: 'revenue', aggFunc: 'spread', }, ], aggregateFunctions: { spread: { compute: ({ values }) => { let min = Infinity; let max = -Infinity; for (const v of values) { if (typeof v === 'number' && Number.isFinite(v)) { if (v < min) min = v; if (v > max) max = v; } } return min === Infinity ? null : max - min; }, }, }, aggregationScope: scope, grandTotal: true, defaultExpandedDepth: 0, aggregateFormatter: ({ value, colId }) => value === null ? '—' : colId === 'revenue' ? `Δ $${Math.round(Number(value)).toLocaleString('en-US')}` : Number(value).toFixed(1), }); const stopTheme = syncGridTheme(stage);
const filterBtn = addButton(controls, 'Filter Compute only', () => { const active = Object.keys(grid.getFilterModel()).length > 0; grid.setFilterModel( active ? {} : { product: { type: 'text', operator: 'equals', value: 'Compute', }, }, ); filterBtn.textContent = active ? 'Filter Compute only' : 'Clear filter'; });
const scopeBtn = addButton(controls, 'Scope: filtered', () => { scope = scope === 'filtered' ? 'all' : 'filtered'; grid.setHierarchyOptions({ rowGrouping: { columns: ['region'], autoColumn: { name: 'Region', width: 170, pinned: 'start', }, }, valueColumns: [ { colId: 'units', aggFunc: 'avg', }, { colId: 'revenue', aggFunc: 'spread', }, ], aggregationScope: scope, grandTotal: true, }); scopeBtn.textContent = `Scope: ${scope}`; meta.textContent = `Aggregation scope: ${scope}`; }); meta.textContent = 'Aggregation scope: filtered';
return () => { grid.destroy(); stopTheme(); };}Built-in Aggregate Functions
Section titled “Built-in Aggregate Functions”aggFunc |
Input | Empty Group Result | Notes |
|---|---|---|---|
'sum' |
Finite numbers | 0 |
Ignores non-numeric values |
'count' |
All participating real rows | 0 |
Counts rows even when cell values are blank |
'min' |
Finite numbers | null |
Minimum finite number |
'max' |
Finite numbers | null |
Maximum finite number |
'avg' |
Finite numbers | null |
Computed from exact numeric sum and count (never average of subgroup averages) |
Custom Functions & aggregationScope
Section titled “Custom Functions & aggregationScope”aggregateFunctions: Register named definitions ({ compute, accumulator? }). Adding an incrementalaccumulator(create,add, optional inverseremove,finish) enables fast updates on edits without rescanning entire groups.aggregationScope:'filtered'(default) aggregates only rows matching active filters;'all'aggregates all source rows within matching groups.aggregateFormatter: Formats synthetic aggregate values (({ value, node, colId, scope }) => string) without invoking ordinary row cell renderers.