Aggregate for dynamic columns

Hi 

Please see the sample here, Sample Code

It's editable grid, all i want is roll up of values (SUM) to show on parent row for progress column. However, my grid has dynamic columns. and there could be multiple columns which needs aggregated values.

Can you guide me on achieving this ?


Thanks.


3 Replies 1 reply marked as answer

PK Padmavathy Kamalanathan Syncfusion Team May 6, 2021 03:59 PM UTC

Hi Parth, 
 
Thanks for contacting Syncfusion Forums. 
 
Query: roll up of values (SUM) to show on parent row for progress column 
 
We can display summed value of children in parent row with the help of valueAccessor property of columns without using Aggregates. We have already discussed the same in the below Knowledge Base help documentation, 
 
We can also use the above solution for dynamic columns and show aggregate for multiple columns.  
 
Please check the below code snippet, 


 
 
  <ejs-treegrid #treegrid [dataSource]='data' idMapping='TaskID' parentIdMapping='parentID'  
    [treeColumnIndex]='1'   (cellEdit)='cellEdit($event)' [columns]='columns'> 
  </ejs-treegrid> 
 
    ngOnInit(): void { 
      ----- 
      this.columns = [ 
        { 
          field: 'TaskID', 
          headerText: 'Task ID', 
          isPrimaryKey: true, 
          width: 90 
        }, 
        { 
          field: 'Progress', 
          headerText: 'Progress Summed to parent', 
          type: 'number', 
          valueAccessor: this.totalChildProgress, 
          width: 100, 
          textAlign: 'Right' 
        } 
        --- 
      ]; 
    } 
  totalChildProgress(field, data, column) { 
    if (!isNullOrUndefined(data.hasChildRecords)) { 
      //checking if the record has children 
      let totalUnitWeight = 0; 
      data.Children.map(row => { 
        //summing child value to show them in parent 
        totalUnitWeight += row['Progress']; 
      }); 
      return totalUnitWeight; 
    } 
    return data.Progress; 
  } 
 
 
 
Please check the below screenshot, 
 
 
 
 
 
Please check the below sample, 
 
Please check the below API help documentation, 
 
If we have misinterpreted your query (or) if the above solution doesn’t meet your requirement, kindly get back to us with below details, 
  1. Detailed explanation of your requirement with screenshot/video demo (if possible)
  2. Complete Tree Grid rendering code
 
Regards, 
Padmavathy Kamalanathan 



PD PDev May 7, 2021 02:48 PM UTC

Thanks this valueaccessor worked but it works while binding the grid. but does not calculate when we edit the cell. i was also expecting that it will update parent total when cell edit is done. Also,  this total does not work in case of multi level total.

Please have a look here for multi level hierarchy



PK Padmavathy Kamalanathan Syncfusion Team May 10, 2021 02:12 PM UTC

Hi Parth, 
 
Thanks for the update. 
 
Query 1: i was also expecting that it will update parent total when cell edit is done. 
Query 2:  Also,  this total does not work in case of multi level total. 
 
We have modifed our solution such that th total works in case of having multi level data and editing. 
 
Please check the below code snippet, 
 
[app.component.ts] 
 
import { findChildrenRecords } from '@syncfusion/ej2-treegrid'; 
 
    totalChildProgress(field, data, column) { 
        if (!isNullOrUndefined(data.hasChildRecords)) { 
          //checking if the record has children 
          let totalUnitWeight = 0; 
          //using findChildrenRecords method to get deep children records and calculate sum for it 
          findChildrenRecords(data).map(row => { 
            //summing child value to show them in parent 
            totalUnitWeight += row['Progress']; 
          }); 
          return totalUnitWeight; 
        } 
        return data.Progress; 
      } 
 
  
Please check the below sample, 
 
Kindly check if the above solution helps to achieve your requirement. If you face any issue in implementing this solution, please get back to us with the below details, 
  1. Screenshots of error with complete stack trace (if any)
  2. Video demo of the issue
  3. If possible, share us issue reproducible sample or reproduce the issue in the sample shared above
 
Regards, 
Padmavathy Kamalanathan 


Marked as answer
Loader.
Up arrow icon