Author Topic: Row Totals for n-Columns  (Read 2101 times)

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Row Totals for n-Columns
« on: August 29, 2020, 02:52:13 am »
Hi. I have a grid that displays a variable number of columns w/costs. What's the best way to have a column at the end of the grid that sums these columns?
In this example I have two columns that I need to sum, but it could be any number of columns

https://jsfiddle.net/cvbwstL2/

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Row Totals for n-Columns
« Reply #1 on: August 31, 2020, 03:40:32 pm »
you may add a property includeInSum: true to those columns and then use a formula to sum the columns.


Code: [Select]
                formulas: [
                  ['sum', function(rd){                 
                    var val = 0;
                    this.getColModel().forEach(function(column){
                      if(column.includeInSum){
                          val += rd[ column.dataIndx ] * 1;
                          }                     
                    })
                    return val;
                  }]
                ],


https://jsfiddle.net/eafr126d/

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Row Totals for n-Columns
« Reply #2 on: August 31, 2020, 04:39:52 pm »
Thank you. Is there a way to apply that at the group level?
https://jsfiddle.net/qpu7fdLj/

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Row Totals for n-Columns
« Reply #3 on: August 31, 2020, 09:10:58 pm »
Formula doesn't work for group title rows.

However end result can be achieved in some cases ( such as yours ) by adding summary: {type: 'sum'} to last column also.

https://jsfiddle.net/stgbvnpr/

mikep

  • Pro Ultimate
  • Full Member
  • *
  • Posts: 163
    • View Profile
Re: Row Totals for n-Columns
« Reply #4 on: August 31, 2020, 09:19:08 pm »
That worked. Thank You!