Total Column

Hi,

I have tree grid with dynamic columns and i intend to add one total column to show ROW Total. 
what will be the right approach to do that 

If you can help me get started! 

3 Replies 1 reply marked as answer

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team April 6, 2021 01:43 PM UTC

Hi Parth, 

Thanks for contacting Syncfusion Support. 

Query#:- I have tree grid with dynamic columns and i intend to add one total column to show ROW Tota 
 
We have checked your query and we can add template column while adding columns dynamically using template property of the Tree Grid as like given below

Refer to the code below:- 
App.Component.html 
<ejs-treegrid #treegrid [dataSource]='data' childMapping='subtasks' [treeColumnIndex]='0' [columns]="treegridColumns"> 
</ejs-treegrid> 
 
 
<ng-template let-data #template1> 
    {{data.taskID + data.progress + data.duration}}  //define the template column here 
</ng-template> 
 
App.component.ts 

  @ViewChild("template1") 
  public temp1: NgModel; 
 
  @ViewChild("treegrid") 
  public treegrid: TreeGrid; 
  public treegridColumns: any; 
 
  ngOnInit(): void { 
    this.data = sampleData; 
  } 
  ngAfterViewInit() { 
    this.treegridColumns = [ 
      { 
        field: "taskID", 
        isPrimaryKey: "true", 
        headerText: "Task ID", 
        width: "90" 
      }, 
      .   .    . 
     { headerText: "taskName", width: "90", template: this.temp1//declare the template column here 
    ]; 
  } 



Documentation and Demo Links:- 

Please get back to us if you need any further assistance. 

Regards, 
Farveen sulthana T 



PD PDev April 29, 2021 12:33 AM UTC

Thanks for the reply. but i think this wont work for me, because i have dynamic column in grid on which i wanted to have total column.

do you have any sample for the same ? Also, it looks like it works on the data and not on column. I need to have functionality which works on the Cell Edit event as well.

I could also see similar example here for JS Sum Column

However what could not understand is "this.model" 

function updateRecord(args){
        
        var data = args.data,
            updatedRecords = this.model.updatedRecords,
            index = updatedRecords.indexOf(data),
            record = updatedRecords[index],
            sum = data.duration + data.progress;
        
       record.sum = record.item.sum = sum;
        this.refreshRow(index);
        
        
      }


FS Farveen Sulthana Thameeztheen Basha Syncfusion Team April 29, 2021 03:40 PM UTC

Hi Parth, 

We have achieved your requirement using actionComplete event of the TreeGrid. In this event we have calculated the values of the required columns and displayed it in the Sum column using setCellValue method of the TreeGrid(as like provided example) 

Refer to the code below:- 

App.Component.html:- 
<ejs-treegrid #treegrid [dataSource] = 'data' childMapping = 'subtasks'[treeColumnIndex] = '0'[columns] = "treegridColumns" 
        (actionComplete) = 'actionComplete($event)'[editSettings] = 'editSettings'[toolbar] = 'toolbar' > 
  </ejs-treegrid > 

App.Component.ts 

export class AppComponent implements OnInit { 
  public data: Object[] = []; 
  public template: any; 
  public editSettings: Object; 
  public toolbar: string[]; 
  @ViewChild("treegrid") 
  public treegrid: TreeGrid; 
  public treegridColumns: any; 
  ngOnInit(): void { 
    this.data = sampleData; 
    this.editSettings = { 
      allowEditing: true, 
      allowAdding: true, 
      allowDeleting: true, 
      mode: "Cell" 
    }; 
    this.toolbar = ["Add", "Delete", "Update", "Cancel"]; 
  } 
  actionComplete(args) { 
    if (args.type == "save") { 
      if (args.column.field == "progress" || args.column.field == "duration") { 
        var total = args.data.progress + args.data.duration; 
        this.treegrid.setCellValue(args.data.taskID, "Sum", total);     //using setCellValue method update the calculative value to Sum column 
      } 
    } 
  } 


Screenshot:- 
 


Note:- this.model is nothing but this.treegrid (i.e treegrid Instance that  you can get from viewChild in EJ2 TreeGrid as like in above sample. 

Please get back to us if you need any further assistance. 

Regards, 
Farveen sulthana T 


Marked as answer
Loader.
Up arrow icon