Custom Aggregate

21 Dec 20221 minute 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.

The custom aggregate function will be invoked with different arguments for total and group aggregations.

  • Total aggregation: The custom aggregate function will be called with the whole data and current AggregateColumn object.
  • Group aggregation: This will be called with the current group details and AggregateColumn object.
@Html.EJS().Grid("DefaultAggregate").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
    col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
    col.Field("Freight").HeaderText("Freight").Width("160").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
    col.Field("ShipCountry").HeaderText("Ship Country").Width("140").Add();

}).AllowPaging().PageSettings(page => { page.PageCount(5); }).Aggregates(agg=> { agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "ShipCountry", Type = "Custom", FooterTemplate = "Brazil Count:${Custom}", CustomAggregate= "customAggregateFn" } }).Add();}).Render()

<script>
    function customAggregateFn(data, aggColumn) {
        return data.result.filter(function (item) {
            return item[aggColumn.columnName] === 'Brazil';
        }).length;
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

NOTE

To access the custom aggregate value inside the template, use the key as Custom.