【问题标题】:Kendo UI Grid built using fluent, client template for loopKendo UI Grid 使用流畅的客户端模板构建循环
【发布时间】:2016-01-29 21:01:30
【问题描述】:

我有一个使用流利的网格构建器构建的剑道网​​格。在我的一个客户端模板中,我需要遍历一个正在返回的集合。这是可疑行:

c.Bound(x => x.EventFormats).Width(300).ClientTemplate("# if(EventFormats != undefined && EventFormats.length > 0){ for(var formatIndex=0; formatIndex < EventFormats.length; formatIndex++){ console.log(i) } } #").EditorTemplateName("Formats");

问题是,当我引入for 循环时,所有地狱都崩溃了,它循环了数十万次(在 300k 之后我放弃并终止了该进程)。

如果我这样做:

c.Bound(x => x.EventFormats).Width(300).ClientTemplate("# if(EventFormats != undefined && EventFormats.length > 0){ console.log(EventFormats.length) } #").EditorTemplateName("Formats");

它正确地返回了我期望在集合中的项目数。只有 4 条正在返回的记录会以集合开头,并且任何集合中的最大项目数为 2(尽管可能更多)。

我不太确定为什么当我介绍 for 循环时它会失去理智,任何见解都将不胜感激。

这是生成的 JSON 对象: [{ "Id":85, "FormatTypeId":34, "Name":"35mm", "Created":"/Date(1447265241983)/", "CreatedBy":"system", "Modified":null, "ModifiedBy":null, "FormatType":null }, { "Id":83, "FormatTypeId":34, "Name":"16mm", "Created":"/Date(1447265241737)/", "CreatedBy":"system", "Modified":"/Date(1453243258067)/", "ModifiedBy":"system", "FormatType":null }]

【问题讨论】:

  • 如果你使用像for (var key in EventFormats) { console.log(EventFormats[key]) } 这样的for .. in 结构,它还会发生吗?
  • @BradWestness JSON 对象的组合方式意味着有额外的键,因此使用for .. in 将意味着更复杂的循环。我必须检查对象中键的索引是否小于length 属性。我将使用 JSON 对象更新问题。
  • 显示EventFormatsjson
  • 我已经用 JSON 更新了这个问题,但也想让大家知道我想出了一个我也发布的解决方案。

标签: kendo-ui kendo-grid


【解决方案1】:

我不完全确定 为什么 它循环了 300k+ 次,但我确实想出了一个可行的解决方案。

我的 KendoGrid 模板现在有这一行:

c.Bound(x => x.Formats).Width(300).ClientTemplate("#=$.generateFormatList(Formats)#");

然后在我的 JS 文件中,我只是这样写了generateFormatList 函数:

$.generateFormatList = function (formats) {
    var template = '';
    if (formats != null) {
        for (var i = 0; i < formats.length; i++) {
            if (i !== 0) {
                template += ", ";
            }
            template += formats[i].Name;
        }
    }
    return template;
}

这就像一个冠军。不知道为什么,但显然 Kendo 无法很好地处理 ClientTemplate 内的 for 循环。

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多