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");
}

Custom Grid summaries

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;
    ....
}

Custom summaries, which access all Grid data

Now you can access all grid data inside the custom column summary. Two additional optional parameters are introduced in the IgxSummaryOperand operate method. As you can see in the code snippet below the operate method has the following three parameters:

  • columnData - gives you an array that contains the values only for the current column
  • allGridData - gives you the whole grid data source
  • fieldName - current column field
class MySummary extends IgxNumberSummaryOperand {
    constructor() {
        super();
    }
    operate(columnData: any[], allGridData = [], fieldName?): IgxSummaryResult[] {
        const result = super.operate(allData.map(r => r[fieldName]));
        result.push({ key: 'test', label: 'Total Discontinued', summaryResult: allData.filter((rec) => rec.Discontinued).length });
        return result;
    }
}

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

Styling

To get started with styling the sorting behavior, we need to import the index file, where all the theme functions and component mixins live:

@import '~igniteui-angular/lib/core/styles/themes/index';

Following the simplest approach, we create a new theme that extends the igx-grid-summary-theme and accepts the $background-color, $focus-background-color, $label-color, $result-color, $pinned-border-width, $pinned-border-style and $pinned-border-color parameters.

$custom-theme: igx-grid-summary-theme(
    $background-color: #e0f3ff,
    $focus-background-color: rgba( #94d1f7, .3 ),
    $label-color: #e41c77,
    $result-color: black,
    $pinned-border-width: 2px,
    $pinned-border-style: dotted,
    $pinned-border-color: #e41c77
);

The last step is to include the component mixins:

@include igx-grid-summary($custom-theme);
Note

If the component is using an Emulated ViewEncapsulation, it is necessary to penetrate this encapsulation using ::ng-deep:

:host {
   ::ng-deep {
       @include igx-grid-summary($custom-theme);
   }
}

Defining a color palette

Instead of hardcoding the color values like we just did, we can achieve greater flexibility in terms of colors by using the igx-palette and igx-color functions.

igx-palette generates a color palette based on the primary and secondary colors that are passed:

$blue-color: #7793b1;
$green-color: #00ff2d;

$my-custom-palette: igx-palette($primary: $blue-color, $secondary: $green-color);

And then with igx-color we can easily retrieve color from the palette.

$custom-theme: igx-grid-summary-theme(
    $background-color: igx-color($my-custom-palette, "primary", 700),
    $focus-background-color: igx-color($my-custom-palette, "primary", 800),
    $label-color: igx-color($my-custom-palette, "secondary", 500),
    $result-color: igx-color($my-custom-palette, "grays", 900),
    $pinned-border-width: 2px,
    $pinned-border-style: dotted,
    $pinned-border-color: igx-color($my-custom-palette, "secondary", 500)
);
Note

The igx-color and igx-palette are powerful functions for generating and retrieving colors. Please refer to Palettes topic for detailed guidance on how to use them.

Using Schemas

Going further with the theming engine, you can build a robust and flexible structure that benefits from schemas. A schema is a recipe of a theme.

Extend one of the two predefined schemas, that are provided for every component, in this case - _light-grid-summary:

// Extending the light grid summary schema
$my-summary-schema: extend($_light-grid-summary,
    (
        background-color: (igx-color: ('primary', 700)),
        focus-background-color: (igx-color: ('primary', 800)),
        label-color: (igx-color: ('secondary', 500)),
        result-color: (igx-color: ('grays', 900)),
        pinned-border-width: 2px,
        pinned-border-style: dotted,
        pinned-border-color: (igx-color: ('secondary', 500))
    )
);

In order to apply our custom schema we have to extend one of the globals (light or dark), which is basically pointing out the components with a custom schema, and after that add it to the respective component themes:

// Extending the global light-schema
$my-custom-schema: extend($light-schema,
    (
        igx-grid-summary: $my-summary-schema
    )
);

// Defining our custom theme with the custom schema
$custom-theme: igx-grid-summary-theme(
    $palette: $my-custom-palette,
    $schema: $my-custom-schema
);

Don't forget to include the themes in the same way as it was demonstrated above.

Demo


API References

Additional Resources

Our community is active and always welcoming to new ideas.