我有Kendo Grid Java脚本分组如下

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


我想得到四个星期的总和

结果如下

最佳答案

您的代码是用JavaScript完成的,这真是太棒了……现在,您可能是最接近(网格中的)上帝的东西了……现在开始工作。

1)转到数据源的模型,然后在其中定义一些方便的计算对象……例如。

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)现在,您只需将这个闪亮的全新计算字段添加到聚合配置中,语法就会有所不同。

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


3)但是,如果您只想让kendo处理网格中的计算属性集合,则会出现一个错误(也许现在已修复)……他错误地尝试将字段名称“ sumOfAllWeeks()”作为函数调用一个蓄能器,不像他在平原上那样。因此,在网格中,您必须将模板用作功能。

让我们通过在网格中为计算所得的新列来说明这一点,如下所示...

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

09-19 14:41