Framework:Javascript Data GridAngular Data GridReact Data GridVue Data Grid

JavaScript Data Grid: Aggregation

When Row Grouping, aggregation functions can be applied to any column to populate the group row with values.

Enabling Aggregation

The simplest way to enable aggregations is with the built-in aggregation functions; sum, min, max, count, avg, first, last.

The following snippet shows how these agg functions can be applied to columns via colDef.aggFunc:

const gridOptions = {
    columnDefs: [
        { field: 'country', rowGroup: true, hide: true },
        { field: 'year', rowGroup: true, hide: true }, 
        { field: 'gold', aggFunc: 'sum' },
        { field: 'silver', aggFunc: 'max' },
        { field: 'bronze', aggFunc: 'avg' },
    ],

    // other grid options ...
}

The example below uses the same built-in aggregation functions shown in the snippet above. Note the following:

  • Rows are grouped by the Country and Year columns by enabling the rowGroup column definition property.
  • The Gold, Silver and Bronze value columns have different agg functions applied via colDef.aggFunc.

The built-in functions will support bigint values if you have them in your data, but the avg function will lose precision as it can only use integer arithmetic if bigint is used.

Customisations

The previous example demonstrated how the built-in agg functions can be used, however extensive Aggregation customisations are also possible as summarised below:

API Reference

Aggregations can be configured using the following column property:

aggFunc
Name of function to use for aggregation. You can also provide your own agg function.
Options: 'sum', 'min', 'max', 'count', 'avg', 'first', 'last'
aggFunc: string | IAggFunc | null;

interface IAggFunc {
    (params: IAggFuncParams) : any
}

interface IAggFuncParams {
  // Values to aggregate 
  values: any[];
  // Column the aggregation function is working on 
  column: Column;
  // ColDef of the aggregation column 
  colDef: ColDef;
  // The parent RowNode, where the aggregation result will be shown 
  rowNode: RowNode;
  // data (if any) of the parent RowNode 
  data: any;
  // The grid api. 
  api: GridApi;
  // The column api. 
  columnApi: ColumnApi;
  // Application context as set on `gridOptions.context`. 
  context: any;
}

Aggregation functions can be registered with the grid using the following grid option:

aggFuncs
{ [key: string]: IAggFunc; }
A map of 'function name' to 'function' for custom aggregation functions.
aggFuncs: { [key: string]: IAggFunc; };

interface IAggFunc {
    (params: IAggFuncParams) : any
}

interface IAggFuncParams {
  // Values to aggregate 
  values: any[];
  // Column the aggregation function is working on 
  column: Column;
  // ColDef of the aggregation column 
  colDef: ColDef;
  // The parent RowNode, where the aggregation result will be shown 
  rowNode: RowNode;
  // data (if any) of the parent RowNode 
  data: any;
  // The grid api. 
  api: GridApi;
  // The column api. 
  columnApi: ColumnApi;
  // Application context as set on `gridOptions.context`. 
  context: any;
}

Next Up

Continue to the next section to learn about Custom Functions.