简体   繁体   English

Kendo-网格-包含复杂对象的聚合

[英]Kendo - Grid - Aggregate with Complex Objects

I have a Kendo UI grid. 我有一个Kendo UI网格。 The grid has a datasource with complex object data. 网格具有包含复杂对象数据的数据源。 For example, {"foo": {"bar" : 10}}. 例如,{“ foo”:{“ bar”:10}}。 Although the column field can navigate the object graph (ie foo.bar), the aggregate field doesn't seem to be able to. 尽管column字段可以导航对象图(即foo.bar),但聚合字段似乎无法。

Here's the code: 这是代码:

var grid = $("#grid").kendoGrid({
   dataSource: {
       data: [
           {"foo": {"bar": 10}},
           {"foo": {"bar": 20}}
       ],

       aggregate: [
           {field: "foo.bar", aggregate: "sum"}
       ]  
   },
   columns: [
       {
           field: "foo.bar",
           footerTemplate: "Sum: #= sum # "
      }
  ]   
}).data("kendoGrid");

Here's the fiddle: http://jsfiddle.net/e6shF/1/ 这是小提琴: http : //jsfiddle.net/e6shF/1/

Firebug reports "TypeError: data.foo is undefined" in line 8 of kendo.all.min.js. Firebug在kendo.all.min.js的第8行中报告“ TypeError:data.foo未定义”。

Am I doing something incorrectly? 我做错了什么吗? If this is a bug in Kendo, is there a way to work around this? 如果这是剑道中的错误,是否可以解决此问题? I have to keep the objects complex. 我必须使对象复杂。

Here's a "better" anwser from Kendo Support: 这是Kendo支持提供的“更好”的答案:

The behavior you are experiencing is caused by the fact that the "path" you have specified will be used as a key in the map created as result of the aggregation. 您遇到的行为是由以下事实造成的:您指定的“路径”将用作聚合结果创建的映射中的键。 Producing a object similar to the following: 产生类似于以下内容的对象:

{ "foo.bar" : { sum: 30 } } {“ foo.bar”:{sum:30}}

Unfortunately, this construct is not supported by the footer template generation and will not be resolved correctly. 遗憾的是,脚注模板生成不支持此构造,因此无法正确解析。 A possible workaround for this scenario is to use a function instead. 这种情况下可能的解决方法是改用函数。 I have modify the sample in order to illustrate this. 为了说明这一点,我修改了样本。

var grid = $("#grid").kendoGrid({
    dataSource: {
        data: [
            {"foo": {"bar": 10}},
            {"foo": {"bar": 20}}
        ],

        aggregate: [
            {field: "foo.bar", aggregate: "sum"}
        ]
    },
    columns: [
        {
            field: "foo.bar",
            footerTemplate: function(data) { return "Sum: " + data["foo.bar"].sum; }
        }
    ]   
}).data("kendoGrid");

It is not possible to have complex objects in aggregates since dynamically generated function for evaluating it, thinks that foo.bar is the name of the field (just one field)? 聚合中不可能有复杂的对象,因为动态生成的评估函数会认为foo.bar是字段的名称(仅一个字段)?

Do you really need that complex field? 您真的需要这个复杂的领域吗?

I might understand that the server (providing the data of the grid) sends that complex foo but you can always flatten it using parse or data functions in the datasource. 我可能理解服务器(提供网格数据)会发送该复杂的foo但您始终可以使用data源中的parsedata函数对其进行展平 Something like this: 像这样:

var grid = $("#grid").kendoGrid({
    dataSource:{
        data:[
            {"foo":{"bar":10}},
            {"foo":{"bar":20}}
        ],
        aggregate:[
            {field:"foo_bar", aggregate:"sum"}
        ],
        schema:   {
            parse:function (data) {
                var res = [];
                $.each(data, function (idx, elem) {
                    res.push({ "foo_bar":elem.foo.bar })
                });
                return res;
            }
        }
    },
    columns:   [
        {
            field:         "foo_bar",
            footerTemplate:"Sum: #= sum # "
        }
    ]
}).data("kendoGrid");

Where I transform received foo.bar into foo_bar and use this for aggregation. 我将接收到的foo.bar转换为foo_bar并将其用于聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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