最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sum of sum columns in kendo grid - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Mar 12, 2015 at 6:25 udaya726 asked Mar 12, 2015 at 6:14 udaya726udaya726 1,0107 gold badges24 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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 puted 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 puted 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 puted 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 puted, 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 puted.
     {
         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;
         }
     }
 ]

You can use a ClientTemplate for this:

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

评论列表(0)

  1. 暂无评论