Grid Summaries Overview

The Angular UI grid in Ignite UI for Angular has a summaries feature that functions on a per-column level as group footer. Angular grid summaries is powerful feature which enables the user to see column information in a separate container with a predefined set of default summary items, depending on the type of data within the column or by implementing a custom angular template in the Grid.

Demo


Note

The summary of the column is a function of all column values, unless filtering is applied, then the summary of the column will be function of the filtered result values

Grid summaries can also be enabled on a per-column level in Ignite UI for Angular, which means that you can activate it only for columns that you need. Grid summaries gives you a predefined set of default summaries, depending on the type of data in the column, so that you can save some time:

For string and boolean data types, the following function is available:

  • count

For number data type, the following functions are available:

  • count
  • min
  • max
  • average
  • sum

For date data type, the following functions are available:

  • count
  • earliest
  • latest

Grid summaries are enabled per-column by setting hasSummary property to true. It is also important to keep in mind that the summaries for each column are resolved according to the column data type. In the igx-grid the default column data type is string, so if you want number or date specific summaries you should specify the dataType property as number or date.

<igx-grid #grid1 [data]="data" [autoGenerate]="false" height="800px" width="800px" (onColumnInit)="initColumn($event)">
    <igx-column field="ProductID" header="Product ID" width="200px"  [sortable]="true">
    </igx-column>
    <igx-column field="ProductName" header="Product Name" width="200px" [sortable]="true" [hasSummary]="true">
    </igx-column>
    <igx-column field="ReorderLevel" width="200px" [editable]="true" [dataType]="'number'" [hasSummary]="true">
    </igx-column>
</igx-grid>

The other way to enable/disable summaries for a specific column or a list of columns is to use the public method enableSummaries/disableSummaries of the igx-grid.

<igx-grid #grid1 [data]="data" [autoGenerate]="false" height="800px" width="800px" (onColumnInit)="initColumn($event)" >
    <igx-column field="ProductID" header="Product ID" width="200px"  [sortable]="true">
    </igx-column>
    <igx-column field="ProductName" header="Product Name" width="200px" [sortable]="true" [hasSummary]="true">
    </igx-column>
    <igx-column field="ReorderLevel" width="200px" [editable]="true" [dataType]="'number'" [hasSummary]="false">
    </igx-column>
</igx-grid>
<button (click)="enableSummary()">Enable Summary</button>
<button (click)="disableSummary()">Disable Summary </button>
public enableSummary() {
    this.grid1.enableSummaries([
        {fieldName: "ReorderLevel", customSummary: this.mySummary},
        {fieldName: "ProductID"}
    ]);
}
public disableSummary() {
    this.grid1.disableSummaries("ProductName");
}

If these functions do not fulfill your requirements you can provide a custom summary for the specific columns. In order to achieve this you have to override one of the base classes IgxSummaryOperand, IgxNumberSummaryOperand or IgxDateSummaryOperand according to the column data type and your needs. This way you can redefine the existing function or you can add new functions. IgxSummaryOperand class provides the default implementation only for the count method. IgxNumberSummaryOperand extends IgxSummaryOperand and provides implementation for the min, max, sum and average. IgxDateSummaryOperand extends IgxSummaryOperand and additionally gives you earliest and latest.

import { IgxSummaryResult, IgxSummaryOperand, IgxNumberSummaryOperand, IgxDateSummaryOperand } from 'igniteui-angular/grid/grid-summary';

class MySummary extends IgxNumberSummaryOperand {

    constructor() {
        super();
    }

    operate(data?: any[]): IgxSummaryResult[] {
        const result = super.operate(data);
        result.push({
            key: 'test',
            label: 'Test',
            summaryResult: data.filter(rec => rec > 10 && rec < 30).length
        });
        return result;
    }
}

In the code above you can see that method operate returns a list of IgxSummaryResult, which is an interface.

interface IgxSummaryResult {
    key: string;
    label: string;
    summaryResult: any;
}
Note

In order to calculate the summary row height properly, the Grid needs the operate method to always return an array of IgxSummaryResult with the proper length even when the data is empty.

And now let's add our custom summary to the column UnitsInStock. We will achieve that by setting the summaries property to the class we create below.

<igx-grid #grid1 [data]="data" [autoGenerate]="false" height="800px" width="800px" (onColumnInit)="initColumn($event)" >
    <igx-column field="ProductID" width="200px"  [sortable]="true">
    </igx-column>
    <igx-column field="ProductName" width="200px" [sortable]="true" [hasSummary]="true">
    </igx-column>
    <igx-column field="UnitsInStock" width="200px" [dataType]="'number'" [hasSummary]="true" [summaries]="mySummary" [sortable]="true">
    </igx-column>
    <igx-column field="ReorderLevel" width="200px" [editable]="true" [dataType]="'number'" [hasSummary]="true">
    </igx-column>
</igx-grid>
...
export class GridComponent implements OnInit {
    mySummary = MySummary;
    ....
}

Summaries with Group By

When you have grouped by columns, the Grid allows you to change the summary position and calculation mode using the summaryCalculationMode and summaryPosition properties.

The available values of the summaryCalculationMode property are:

  • rootLevelOnly - Summaries are calculated only for the root level.
  • childLevelsOnly - Summaries are calculated only for the child levels.
  • rootAndChildLevels - Summaries are calculated for both root and child levels. This is the default value.

The available values of the summaryPosition property are:

  • top - The summary row appears before the group by row children.
  • bottom - The summary row appears after the group by row children. This is the default value.
Note

The summaryPosition property applies only for the child level summaries. The root level summaries appear always fixed at the bottom of the Grid.

Demo


Keyboard Navigation

The summary rows can be navigated with the following keyboard interactions:

  • UP - navigates one cell up
  • DOWN - navigates one cell down
  • LEFT - navigates one cell left
  • RIGHT - navigates one cell right
  • CTRL + LEFT or HOME - navigates to the leftmost cell
  • CTRL + RIGHT or END - navigates to the rightmost cell
  • TAB - sequentially navigates to the next cell on the row and if the last cell is reached navigates to the next row
  • SHIFT + TAB - sequentially navigates to the previous cell on the row and if the first cell is reached navigates to the previous row

API References

Additional Resources

Our community is active and always welcoming to new ideas.