简体   繁体   中英

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 在此处输入图片说明

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;
         }
     }
 ]

您可以为此使用ClientTemplate

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM