demo D67
The aggregations in Angular
sum, avg, min, max, count, countValues, first, last, one column each
total: 'sum'
This grid shows the built-in aggregations one per column: sum, average, min, max, count, count of values, first and last. It reduces a column, or a group, to the single figure someone asked for, so a footer answers the question without a separate calculation.
In Angular, an aggregation is one key on the column definition: total set to 'sum', 'avg', 'min', 'max' or 'count', and 'countValues', 'first' or 'last' for text columns. The standalone LatticeGridComponent from @toclocoinc/lattice-grid/angular takes the columns and grandTotalRow through its config input, so the figures sit in a row pinned under the viewport. The grid above groups twenty thousand service records by team, one reduction per column. The Angular code underneath is the same configuration, with the notes after it on outputs and reading totals back.
Install and import
npm install @toclocoinc/lattice-grid
# free on localhost; a production domain needs a licence key (see /pricing/)
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { LatticeGridComponent, provideLattice } from '@toclocoinc/lattice-grid/angular';
The configuration
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { LatticeGridComponent, provideLattice } from '@toclocoinc/lattice-grid/angular';
// One reduction per column, named on total: sum, avg, min, max and count for
// numbers, and countValues, first and last for the rest. Each rolls up in the
// grand total row and, with a grouped column, in every group's subtotal.
const columns = [
{ field: 'team', title: 'Team', group: { enabled: true, index: 0 } },
{ field: 'service', title: 'Service', total: 'count' },
{ field: 'instances', title: 'sum', type: 'number', total: 'sum' },
{ field: 'p95Ms', title: 'avg', type: 'number', total: 'avg' },
{ field: 'p50Ms', title: 'min', type: 'number', total: 'min' },
{ field: 'p99Ms', title: 'max', type: 'number', total: 'max' },
{ field: 'owner', title: 'countValues', total: 'countValues' },
{ field: 'region', title: 'first', total: 'first' },
{ field: 'tier', title: 'last', total: 'last' },
];
const rows = [/* 20,000 service records */];
@Component({
selector: 'app-root',
standalone: true,
imports: [LatticeGridComponent],
template: '<lattice-grid [config]="grid" style="display:block;height:540px"></lattice-grid>',
})
export class AppComponent {
grid = {
rowKey: 'id',
grandTotalRow: 'bottom',
columns: columns,
rows: rows,
};
}
bootstrapApplication(AppComponent, {
providers: [provideLattice({ createGrid })],
});
API notes for aggregations in Angular
- Declaring an aggregate. Put
totalon the column definition insideconfig. The built-in names aresum,min,max,avg,count,first,last,countValuesandmedian. The demo uses one of each of the first eight, one per column.countValuescounts the non-blank values, andfirstandlasttake the value at the ends of the group in its current order.{ fn: 'percentile', p: 0.9 }is accepted wherever a name is, and so is a function(values, ctx) => resultfor your own reduction. - Different figures for groups and the grand total.
totalapplies to both.groupTotaloverrides it for group subtotals andgrandTotalfor the grand total row only. - Turning on the grand total. Set
grandTotalRowin the config object.'bottom', as in the demo, pins the row beneath the viewport so it stays in view while the rows scroll and is not counted byrows.count().truemakes it the last display row instead, and it is counted. - Turning on group footers. Group a column with
group: { enabled: true, index: 0 }, asteamis here, and every group row carries the reductions of the columns that have atotal. AddgroupFooter: trueto the config for a totals line at the foot of each group. - Inputs.
[config]takes every configuration key, includingcolumnsandrows.[sort],[filters],[quickFilter]and[selectedKeys]are applied through the matching grid API. Give the<lattice-grid>element a height, as the demo’sheight:540pxdoes. - Outputs. Every grid event is an output under its kebab-case name.
(group-toggled)fires as a group opens or closes and(totals-contextmenu)when the pinned totals row is right-clicked, carrying thecolId.(grid-ready)hands you the grid instance. - Reading a total back. Take the instance from a
viewChildor from(grid-ready). A group row’s own reductions are inrow.totals, keyed by column id, on the row fromgrid.rows.get(index)whererow.groupis true. WithgrandTotalRow: truethe grand total is the last display row,grid.rows.get(grid.rows.count() - 1). - Changing an aggregate at run time.
grid.columns.setTotal('instances', 'avg')changes a column, and{ scope: 'group' }or{ scope: 'grand' }as the third argument changes one scope only. - The gotcha. Inputs are compared by identity (
Object.is) and never rebuild the grid. An inline[config]="{ columns: columns }"is a new object on every change detection pass, so hold the configuration in a field or a signal, as the code above does withgrid.
How do I read a group’s total in Angular?
Declare grid = viewChild<LatticeGridComponent>('grid') with #grid on the element, or use (grid-ready). The grid paints on its own schedule rather than Angular’s, so read it from a grid event or an event handler, not in ngAfterViewInit. Then read row.totals on a group row.
Can the grand total sum while each group averages in Angular?
Yes. Declare { field: 'p95Ms', total: 'avg', grandTotal: 'sum' } in the columns, or the reverse with groupTotal. The override applies to its own scope only.
Do the totals update when I replace the rows in the config?
A changed input is pushed into the grid already on screen from ngOnChanges and does not rebuild it, so the totals follow the new rows while scroll position and selection stay. Replace the config object with a new one holding the new rows, rather than pushing into the old array.
Summarising a column with sum, avg, min, max, count and more
An aggregation reduces every value in a column, or every value within a group, down to a single figure: a sum of “Revenue”, the average of “Response time”, the earliest “first” value or the latest “last” one. A developer reaches for it wherever a column of raw numbers needs a headline figure alongside it, whether that is a grand total row, a per-group subtotal, or a pivoted summary table. Lattice Grid assigns the function per column with total: 'sum', and the same setting accepts 'avg', 'min', 'max', 'count', 'countValues', 'first' or 'last', so a column of currency values and a column of timestamps can each carry the aggregation that actually suits their data rather than sharing one formula. Because this is a JavaScript data grid built around incremental state rather than repeated full-table scans, adding or removing a row updates the affected totals directly instead of walking every row again, which keeps aggregation cost proportional to the change rather than to the size of the dataset. Grouping, group footers and the grand total row all read from these same per-column definitions, so a sum defined once is reused everywhere a total for that column is displayed, with no separate calculation to keep in sync.
How do I calculate a sum or average for a column in a data grid?
Set total: 'sum' on the column definition, or 'avg', 'min', 'max', 'count', 'countValues', 'first', 'last' for the other built-in reducers. Lattice Grid applies the chosen function to the column’s values, and reuses that same definition for group footers, the grand total row and pivot summaries, so the aggregation only needs to be declared once per column.