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

Aggregates in Expression Column

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-columns>
                        <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>
                    </e-columns>
                </ejs-grid>

1 Reply

PS Pavithra Subramaniyam Syncfusion Team 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: 

[app.component.html] 
. . .  
<ejs-grid [dataSource]='data'> 
    <e-columns> 
      <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-columns> 
    <e-aggregates> 
      <e-aggregate> 
        <e-columns> 
          <e-column columnName="Dummy" type="Custom" [customAggregate]="customAggregateFn"> 
            <ng-template #footerTemplate let-data>Sum: {{data.Custom}}</ng-template> 
          </e-column> 
        </e-columns> 
      </e-aggregate> 
    </e-aggregates> 
  </ejs-grid> 
. . .  

[App.component.ts] 

. . . 
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. 
 
Regards, 
Pavithra S. 


Loader.
Live Chat Icon For mobile
Up arrow icon