udaya726
udaya726

Reputation: 1020

Sum of sum columns in kendo grid

I have kendo grid java script grouping as follows

group: {
                        field: "ResourceName",
                        aggregates: [
                           { field: "WeekOneUtilization", aggregate: "sum" },
                            { field: "WeekTwoUtilization", aggregate: "sum" },
                             { field: "WeekThreeUtilization", aggregate: "sum" },
                              { field: "WeekFourUtilization", aggregate: "sum" }
                        ]
                    },
                    aggregate: [{ field: "ProjectName", aggregate: "count" },
                                { field: "WeekOneUtilization", aggregate: "sum" },
                            { field: "WeekTwoUtilization", aggregate: "sum" },
                             { field: "WeekThreeUtilization", aggregate: "sum" },
                              { field: "WeekFourUtilization", aggregate: "sum" }]

I want to get Sum of sums of four weeks

the result is as follows enter image description here

Upvotes: 1

Views: 10262

Answers (2)

Adrian Salazar
Adrian Salazar

Reputation: 5319

Your code is done in JavaScript so that's awesome... now you're the closest thing to god (of your grid) as you could be... so let's get to work.

1) Go to your datasource's model, and define some handy computed things there... for example.

var yourDataSource = new kendo.data.DataSource({
    schema:{
        model: {
            // Typical blah blah here
            id: 'your-id-field',
            fields: {
               // Typical field declaration blah blah
            },
            // This handy function will calculate
            // the sum of the 4 weeks in your row
            sumOfAllWeeks: function(){                    
               return this.get('WeekOneUtilization') + 
                      this.get('WeekTwoUtilization') + 
                      this.get('WeekThreeUtilization') + 
                      this.get('WeekFourUtilization');                 
            }
        }     
    }
});

2) Now you can just add this shiny brand new computed field into your aggregates configuration, the syntax is a bit different.

{ field: "sumOfAllWeeks()", aggregate: "sum" }

3) However, there is a bug (maybe its fixed now) if you want to just let kendo deal with aggregates of computed properties in your grid... he mistakenly tries to invoke your field name "sumOfAllWeeks()" as a function instead of an accumulator, unlike he does with a plain field. So in your grid, you have to use the templates as a function.

Let's illustrate this by creating a new column for your computed, in your grid, like this...

 // Your grid fields declaration
 columns:[
     // Here you may have your columns declared,
     // as shown in your picture... so I save the blah blah
     // Now we declare a column for your computed.
     {
         field: 'sumOfAllWeeks()',
         aggregates: 'sum', 
         // This handles the group footer template
         groupFooterTemplate: function(data){
           return data['sumOfAllWeeks()'].sum;
         },
         // This handles the table footer template
         footerTemplate: function(data){
           return data['sumOfAllWeeks()'].sum;
         }
     }
 ]

Upvotes: 2

hutchonoid
hutchonoid

Reputation: 33306

You can use a ClientTemplate for this:

columns.Bound(p => p.Total).ClientTemplate("#= WeekOneUtilization + WeekTwoUtilization + WeekThreeUtilization + WeekFourUtilization #");

Upvotes: 0

Related Questions