How can I help you?
Footer Aggregate in Angular Grid Component
19 Mar 202611 minutes to read
The Syncfusion® Angular Grid supports calculating and displaying aggregate values in the footer cells. These footer aggregates are computed from all rows in the grid. The footerTemplate property can be used to customize the rendering of aggregate values in the footer cells, allowing formatted text or custom layouts to be displayed.
import { data } from './datasource';
import { Component, OnInit } from '@angular/core';
import { AggregateService, GridModule } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [ GridModule ],
providers: [AggregateService],
standalone: true,
selector: 'app-root',
templateUrl: 'app.template.html'
})
export class AppComponent implements OnInit {
public data?: object[];
ngOnInit(): void {
this.data = data;
}
}<ejs-grid [dataSource]='data' height='210px'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='Freight' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
<e-aggregates>
<e-aggregate>
<e-columns>
<e-column field="Freight" type="sum">
<ng-template #footerTemplate let-data>Sum: {{data.sum}}</ng-template>
</e-column>
</e-columns>
</e-aggregate>
<e-aggregate>
<e-columns>
<e-column field="Freight" type="max">
<ng-template #footerTemplate let-data>Max: {{data.max}}</ng-template>
</e-column>
</e-columns>
</e-aggregate>
</e-aggregates>
</ejs-grid>import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Inside the template, access each aggregate value using its type name. For example, use
data.sumto access the sum aggregate and data.max to access the maximum aggregate.
Format aggregate value
Aggregate values displayed in footer cells can be formatted using the format property of the AggregateColumnDirective. This property accepts a format string that defines the appearance of the aggregate value, such as specifying currency, number of decimal places, or percentage format.
import { data } from './datasource';
import { Component, OnInit } from '@angular/core';
import { AggregateService, GridModule } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [ GridModule],
providers: [AggregateService],
standalone: true,
selector: 'app-root',
templateUrl: 'app.template.html'
})
export class AppComponent implements OnInit {
public data?: object[];
ngOnInit(): void {
this.data = data;
}
}<ejs-grid [dataSource]="data" height="210px">
<e-columns>
<e-column field="OrderID" headerText="Order ID" textAlign="right" width="120"></e-column>
<e-column field="CustomerID" headerText="Customer ID" width="150"></e-column>
<e-column field="Freight" width="150"></e-column>
<e-column field="ShipName" headerText="Ship Name" width="150"></e-column>
</e-columns>
<e-aggregates>
<e-aggregate>
<e-columns>
<e-column field="Freight" type="sum" format="N0">
<ng-template #footerTemplate let-data>Sum: {{ data.sum }}</ng-template>
</e-column>
</e-columns>
</e-aggregate>
<e-aggregate>
<e-columns>
<e-column field="Freight" type="max" format="N0">
<ng-template #footerTemplate let-data>Max: {{ data.max }}</ng-template>
</e-column>
</e-columns>
</e-aggregate>
</e-aggregates>
</ejs-grid>import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Display aggregates in the header
By default, aggregate values are displayed at the bottom of the grid in the footer section. It is also possible to place these values at the top of the header. This can be achieved by handling the dataBound event of the Grid and using the getHeaderContent, and getFooterContent methods. In this approach, the footer content is programmatically appended to the header content once the grid has finished rendering.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { GridModule } from '@syncfusion/ej2-angular-grids'
import { AggregateService } from '@syncfusion/ej2-angular-grids'
import { Component, OnInit, ViewChild } from '@angular/core';
import { data } from './datasource';
import { GridComponent } from '@syncfusion/ej2-angular-grids';
@Component({
imports: [GridModule],
providers: [AggregateService],
standalone: true,
selector: 'app-root',
template: `<ejs-grid #grid [dataSource]='data' height='210px' (dataBound)="dataBound()">
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='Freight' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
<e-aggregates>
<e-aggregate>
<e-columns>
<e-column field="Freight" type="sum">
<ng-template #footerTemplate let-data>Sum: {{data.sum}}</ng-template>
</e-column>
</e-columns>
</e-aggregate>
<e-aggregate>
<e-columns>
<e-column field="Freight" type="max">
<ng-template #footerTemplate let-data>Max: {{data.max}}</ng-template>
</e-column>
</e-columns>
</e-aggregate>
</e-aggregates>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data?: object[];
@ViewChild('grid')
public grid?: GridComponent;
ngOnInit(): void {
this.data = data;
}
dataBound() {
(this.grid as GridComponent).getHeaderContent().append((this.grid as GridComponent).getFooterContent());
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));