Aggregate all page

Hi Team!

It is possible to aggregate a column from the whole datasource, not just the current page? I have a custom aggregate function, but I can only get the current page data from the data attribute.

Thank you!

1 Reply 1 reply marked as answer

PG Praveenkumar Gajendiran Syncfusion Team May 10, 2021 12:58 PM UTC

Hi Kulcsár,  

Greetings from Syncfusion support. 

Query : It is possible to aggregate a column from the whole datasource, not just the current page? I have a custom aggregate function, but I can only get the current page data from the data attribute..  
 
Before proceeding your query we would like to share some behavior of EJ2 Grid Aggregates. 

By default in EJ2 Grid, the Aggregates are calculated for entire Grid data when the grid contains local dataSource. If the Grid having remote data then you need to calculate aggregates at server side and return the aggregate value to grid as like following code example. 

The below code is used for Url adaptor. 

  public IActionResult UrlDatasource([FromBody]DataManagerRequest dm) 
        { 
            IEnumerable DataSource = OrdersDetails.GetAllRecords(); 
            DataOperations operation = new DataOperations(); 
            . . . .  
            List<string> str = new List<string>(); 
            if (dm.Aggregates != null) 
            { 
                for (var i = 0; i < dm.Aggregates.Count; i++) 
                    str.Add(dm.Aggregates[i].Field); 
            } 
            IEnumerable aggregate = operation.PerformSelect(DataSource, str); 
            int count = DataSource.Cast<OrdersDetails>().Count(); 
            if (dm.Skip != 0) 
            { 
                DataSource = operation.PerformSkip(DataSource, dm.Skip);   //Paging 
            } 
            if (dm.Take != 0) 
            { 
                DataSource = operation.PerformTake(DataSource, dm.Take); 
            } 
            return dm.RequiresCounts ? Json(new { result = DataSource, count = count, aggregate = aggregate }) : Json(DataSource); 
        } 

When using custom aggregates in the grid, you can get only the current page records in the custom aggregate function’s arguments.  

Based on your query we understood that you need to perform calculation on entire grid dataSource in the “customAggregateFn”.  If you are using local data, you can use the following workaround to calculate aggregate value from entire data by using CustomAggregateFn (because we can get entire data from grid.dataSource).  This is demonstrated in the below code example. 

var customAggregateFn = function(data) { 
  //we can get the current page records from its args -data 
  var gridData = grid.dataSource; // get the entire grid datasource by using gridObj.dataSource  which returns entire data if the grid having local dataSource 
 var totalSum = 0; 
// do your calculation as you want with the entire dataset 
  for (var i = 0; i < gridData.length; i++) { 
    totalSum = totalSum + gridData[i]['Freight']; 
  } 
  return totalSum; // return the calculated value 

}; 
var grid = new ej.grids.Grid({ 
  dataSource: window.orderData, 
  allowPaging: true, 
  pageSettings: { pageCount: 5 }, 
  columns: [ 
    { field: 'CustomerName', headerText: 'Customer Name', width: 150 }, 
    { field: 'Freight', width: 160, format: 'C2', textAlign: 'Right' }, 
    { 
      field: 'OrderDate', 
      headerText: 'Order Date', 
      width: 130, 
      format: 'yMd', 
      textAlign: 'Right' 
    }, 
    { field: 'ShipCountry', headerText: 'Ship Country', width: 140 } 
  ], 
  aggregates: [ 
    { 
      columns: [ 
        { 
          type: 'Sum', 
          field: 'Freight', 
          format: 'C2', 
          footerTemplate: 'Sum: ${Sum}' 
        } 
      ] 
    }, 
    { 
      columns: [ 
        { 
          type: 'Custom', 
          format: 'C2', 
          customAggregate: customAggregateFn, 
          field: 'Freight', 
          footerTemplate: 'Custom: ${Custom}' 
        } 
      ] 
    } 
  ] 
}); 
grid.appendTo('#Grid'); 


Note : When using remote data in grid, we have only knowledge about current page records. so we cannot get the entire data of Grid. So, we can calculate aggregate value from the current page records only when using customAggregate function. 

We have prepared a sample based on this for your reference.  


Please get back to us if you need further assistance on this. 

Regards,  
Praveenkumar G 


Marked as answer
Loader.
Up arrow icon