We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Aggregates in Expression Column

Thread ID:





145736 Jul 5,2019 12:36 PM UTC Jul 8,2019 08:53 AM UTC Angular - EJ 2 1
Tags: Grid
Samir Modi
Asked On July 5, 2019 12:36 PM UTC

Hi Team,
How can we have a aggregate (sum) for below expression column 'Calories Intake' . I believe aggregate needs a field attribute to perform aggregration and since expression column is derived on the fly , is it possible to to achieve this?

<ejs-grid [dataSource]='data' [height]='315'>
                        <e-column field='FoodName' headerText='Food Name' width=150></e-column>
                        <e-column field='Protein' headerText='Protein' textAlign='Right' width=120></e-column>
                        <e-column field='Fat' headerText='Fat' textAlign='Right' width=80></e-column>
                        <e-column field='Carbohydrate' headerText='Carbohydrate' textAlign='Right' width=120></e-column>
                        <e-column headerText='Calories Intake' textAlign='Right' [valueAccessor]='totalCalories' width=150></e-column>

Pavithra Subramaniyam [Syncfusion]
Replied On July 8, 2019 08:53 AM UTC

Hi Samir, 

Thanks for contacting Syncfusion. 

By default the aggregate values cannot be calculated for the column which is not in the dataSource. And also valueAccessor is used for change the cell text only. So it does not affect the Grid data source. This is the default behavior of the Grid.  However you can achieve your requirement in below way: 

We suggest you to use custom aggregate for the Expression column to display aggregates.  By default when we use customAggregate with paging we will only get the current page results. If you want to get whole dataSource in initial rendering we need to set disablePageWiseAggregates as true in groupSettings property of the Grid .You can access the grid dataSource  within customAggregate and you can perform your own calculation based on your requirement using that dataSource. 
Please find the below code snippet: 

. . .  
<ejs-grid [dataSource]='data'> 
      <e-column field='FoodName' headerText='Food Name' width=150></e-column> 
      // you can provide  dummy field for to generate custom aggregates 
      <e-column field="Dummy" headerText='Calories Intake' textAlign='Right' [valueAccessor]='totalCalories' width=150></e-column> 
       .  . 
          <e-column columnName="Dummy" type="Custom" [customAggregate]="customAggregateFn"> 
            <ng-template #footerTemplate let-data>Sum: {{data.Custom}}</ng-template> 
. . .  


. . . 
public totalCalories = (field: string, data: { Protein: number, Fat: number, Carbohydrate: number }, column: Object): number => { 
    return data.Protein * 4 + data.Fat * 9 + data.Carbohydrate * 4; 

  public customAggregateFn = (sdata: any) => { 
    let sum = 0; 
    sdata.result.forEach((e) => { 
      sum = sum + (e.Protein * 4 + e.Fat * 9 + e.Carbohydrate * 4); 
    return sum; 
. . .  

Note: When paging enabled we will get the whole dataSource in initial loading. After paging performed we will get only current page results in customAggregateFn. 

We have prepared a sample based on your requirement. Please find the below sample: 

Sample                     : https://stackblitz.com/edit/angular-zlvks3-gvuneq?file=app.component.html 

Please get back to us if you need further assistance. 
Pavithra S. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon