Custom aggregate in Angular TreeGrid component
25 Aug 20254 minutes to read
To calculate aggregate values using your own aggregation logic, use the custom aggregate feature in the TreeGrid. To enable custom aggregation, set the type
property to Custom
and assign your custom aggregation function to the customAggregate
property.
import { NgModule,ViewChild } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeGridModule } from '@syncfusion/ej2-angular-treegrid'
import { AggregateService } from '@syncfusion/ej2-angular-treegrid'
import { Component, OnInit } from '@angular/core';
import { summaryData } from './datasource';
import { getObject, CustomSummaryType } from '@syncfusion/ej2-grids';
@Component({
imports: [
TreeGridModule
],
providers: [AggregateService ],
standalone: true,
selector: 'app-container',
template: `<ejs-treegrid [dataSource]='data' height='245' [treeColumnIndex]='0' childMapping='subtasks' >
<e-columns>
<e-column field='category' headerText='Category' width=240></e-column>
<e-column field='units' headerText='Total Units' textAlign='Right' type='number' Width=130></e-column>
<e-column field='unitPrice' headerText='Unit Price($)' format='C2' textAlign='Right' type='number' width=110 ></e-column>
<e-column field='price' headerText='Price($)' textAlign='Right' type='number' width=160 ></e-column>
</e-columns>
<e-aggregates >
<e-aggregate [showChildSummary]='false' >
<e-columns>
<e-column field="price" format='C2' type="Custom" [customAggregate]='customAggregateFn' columnName='category' >
<ng-template #footerTemplate let-data>Count of Frozen seafood : </ng-template>
</e-column>
</e-columns>
</e-aggregate>
</e-aggregates>
</ejs-treegrid>`
})
export class AppComponent implements OnInit {
public data?: Object[];
ngOnInit(): void {
this.data = summaryData;
}
customAggregateFn (data: Object): number {
let sampleData: Object[] = getObject('result', data);
let countLength: number; countLength = 0;
sampleData.filter((item: Object) => {
let data: string = getObject('category', item);
if (data === 'Frozen seafood') {
countLength++;
}
});
return countLength;
};
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
To access the custom aggregate value inside a template, use the key
Custom
within the context of your footer or summary template.