Custom aggregate in EJ2 JavaScript Treegrid control
27 Apr 20235 minutes to read
To calculate the aggregate value with your own aggregate functions, use the custom aggregate option. To use custom aggregation, specify the type
as Custom
, and provide the custom aggregate function in the customAggregate
property.
ej.treegrid.TreeGrid.Inject(ej.treegrid.Aggregate);
var customAggregateFn = function(data){
var sampleData = ej.grids.getObject('result', data);
var countLength = 0;
sampleData.filter((item) => {
var data = ej.grids.getObject('category', item);
if (data === 'Frozen seafood') {
countLength++;
}
});
return countLength;
};
var treeGridObj = new ej.treegrid.TreeGrid({
dataSource: summaryData,
childMapping: 'subtasks',
width: 'auto',
height: 245,
treeColumnIndex: 1,
columns: [
{ field: 'category', headerText: 'Category', width: 200 },
{ field: 'units', headerText: 'Total Units', width: 130, type: 'number', textAlign: 'Right' },
{ field: 'unitPrice', headerText: 'Unit Price($)', width: 110, type: 'number', format: 'C2', textAlign: 'Right' },
{ field: 'price', headerText: 'Price($)', width: 110, textAlign: 'Right', type: 'number', format: 'C' },
],
aggregates: [{
showChildSummary: false,
columns: [{
type: 'Custom',
customAggregate: customAggregateFn,
columnName: 'category',
footerTemplate: 'Count of Frozen seafood : ${Custom}'
}]
}]
});
treeGridObj.appendTo('#TreeGrid');
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Grid</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Grid Control">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-grids/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-treegrid/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-calendars/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/23.1.36/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/23.1.36/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="TreeGrid"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
To access the custom aggregate value inside the template, use the key as
Custom
.