Search results

Aggregates

Aggregate values are displayed in the footer, group footer, or group caption of the Grid. It can be configured through Aggregates property. Field and Type are the minimum properties required to represent an aggregate column.

By default, the aggregate value can be displayed in the footer, group, and caption cells. To show the aggregate value in one of the cells, use the FooterTemplate, GroupFooterTemplate, or GroupCaptionTemplate property.

Built-in aggregate types

The aggregate type should be specified in the Type property to configure an aggregate column.

The built-in aggregates are,

  • Sum
  • Average
  • Min
  • Max
  • Count
  • TrueCount
  • FalseCount
  • Multiple aggregates can be used for an aggregate column by setting the Type property with an array of aggregate types.
  • Multiple types for a column is supported only when one of the aggregate templates is used.

Footer aggregate value is calculated for all the rows, and it is displayed in the footer cells. Use the FooterTemplate property to render the aggregate value in footer cells.

razor
footer-agg.cs
@Html.EJS().Grid("Footer").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 = "Freight", Type = "Sum", FooterTemplate = "Sum: ${Sum}" } }).Add();
        agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Type = "Average", FooterTemplate = "Average: ${Average}" } }).Add(); }).Render()
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

The aggregate values must be accessed inside the template using their corresponding Type name.

How to format aggregate value

You can format the aggregate value result by using the Format property.

razor
format-agg.cs
@Html.EJS().Grid("FormatAggregate").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 = "Freight", Format = "C2", Type = "Sum", FooterTemplate = "Sum: ${Sum}" } }).Add();
    agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Average", FooterTemplate = "Average: ${Average}" } }).Add(); }).Render()
public IActionResult Index()
{
    var Order = Product.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Group and caption aggregate

Group and caption aggregate values are calculated from the current group items. If GroupFooterTemplate is provided, the aggregate values are displayed in the group footer cells; and if GroupCaptionTemplate is provided, aggregate values are displayed in the group caption cells.

razor
group-caption.cs
@Html.EJS().Grid("GroupCaption").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); }).AllowGrouping().GroupSettings(new Syncfusion.EJ2.Grids.GridGroupSettings() { Columns = new string[] { "ShipCountry" } }).Aggregates(agg=> { agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>
        () { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Sum", GroupFooterTemplate = "Sum: ${Sum}" } }).Add();
        agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Average", GroupCaptionTemplate = "Average: ${Average}" } }).Add(); }).Render()
public IActionResult Index()
{
    var Order = Product.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

The aggregate values must be accessed inside the template using their corresponding Type name.

Custom aggregate

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.
razor
@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) {
        return data.result.filter(function (item) {
            return item['ShipCountry'] === 'Brazil';
        }).length;
    }
</script>

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

Reactive aggregate update

When using batch editing, the aggregate values will be refreshed on every cell save. The footer, group footer, and group caption aggregate values will be refreshed.

Adding a new record to the grouped grid will not refresh the aggregate values.

razor
reactive-agg-batch-edit.cs
@Html.EJS().Grid("Grid").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); }).AllowGrouping().GroupSettings(new Syncfusion.EJ2.Grids.GridGroupSettings() { Columns = new string[] { "ShipCountry" } }).Aggregates(agg=> { agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>
        () { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Sum", GroupFooterTemplate = "Sum: ${Sum}" } }).Add();
        agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Average", GroupCaptionTemplate = "Average: ${Average}" } }).Add();
        agg.Columns(new List<Syncfusion.EJ2.Grids.GridAggregateColumn>() { new Syncfusion.EJ2.Grids.GridAggregateColumn() { Field = "Freight", Format = "C2", Type = "Sum", FooterTemplate = "Sum: ${Sum}" } }).Add(); }).Toolbar(new List<string>() 
        { "Delete", "Update", "Cancel" }).EditSettings(edit => { edit.AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Batch); }).Render()
public IActionResult Index()
{
    var Order = Product.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Refresh aggregates in inline edit mode

By default, reactive aggregate update is not supported by inline and dialog edit modes as it is not feasible to anticipate the value change event for every editor. But, you can refresh the aggregates manually in the inline edit mode using the refresh method of aggregate module.

In the following code, the input event for the Freight column editor has been registered and the aggregate value has been refreshed manually.

razor
reactive-agg-inline-edit.cs
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).ActionBegin("actionBegin").Columns(col =>
{
    col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
    col.Field("Freight").HeaderText("Freight").Width("160").Format("C2").EditType("numericedit").Edit(new { @params = new { change = "changeFn" } }).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 = "Freight", Format = "C2", Type = "Sum", FooterTemplate = "Sum: ${Sum}" } }).Add(); }).Toolbar(new List<string>() 
        { "Delete", "Update", "Cancel" }).EditSettings(edit => { edit.AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Render()

    <script>            
            let selectedRecord = {};
             function actionBegin(args) {
            if ((args.requestType === 'beginEdit')) {
                selectedRecord = {};
                selectedRecord = args.rowData;
             }
            }
            function changeFn(args){
                let gridObj = document.getElementById("Grid").ej2_instances[0];
                selectedRecord['Freight'] = args.value;
                gridObj.aggregateModule.refresh(selectedRecord);
            }

    </script>
public IActionResult Index()
{
    var Order = Product.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}