2016-11-29 59 views
1

我有很多喜欢这里描述的是什么,但我的目标是不是方便标记的数字显示故障数据:How do I iterate over an unknown object in a Meteor Spacebars template?迭代不明物体流星

数据可能被嵌套一次以上,并可能会有所不同,但最后总是在一个数组中。 我也接受新的数据结构。

样本数据:

{ 
    "Category A": 
     { 
      "Sub-Category1": 
       { 
        "Sub-Sub-Category1": ['Value1', 'Value2'] 
       }, 
      "Sub-Category2": 
       { 
        "Sub-Sub-Category2": ['Value3'], 
        "Sub-Sub-Category3": ['Value4'] 
       } 
    }, 
    "Category B": 
     { 
      "Sub-Category1": ['Value5'] 
    } 
} 
+0

请具体谈谈您想达到的目标。你已经尝试了什么?你遇到什么问题? – Mikkel

回答

1

你会需要一个递归模板来处理任意嵌套,枚举对象的密钥的帮手,并得到母公司的价值帮手对象对应于一个键。

HTML:

<template name="nest"> 
{{#if isArray}} 
    {{#each this}} 
    {{this}} <!-- we're assuming that array elements aren't themselves objects --> 
    {{/each}} 
{{#elseif isObject}} 
    {{#each keys}} 
    {{#with value}} 
     {{> nest }} 
    {{/with}} 
    {{/each}} 
{{else}} <!-- catch the case of a scalar key value (no array) --> 
    {{this}} 
{{/if}} 
</template> 

JS:

Template.nest.helpers({ 
    isArray(){ 
    return typeof(this) === "object" && this.length && this.length > 0; 
    }, 
    isObject(){ 
    return typeOf(this) === "object" && !this.length; 
    }, 
    keys(){ 
    return Object.keys(this); // returns an array of strings 
    }, 
    value(){ 
    return Template.parentData()[this]; // look up the hierarchy to get the parent object then select the data for the current key 
    } 
}); 
+0

谢谢,它的工作原理!然而,我的数据是相当大的,它是永久加载,是否有任何建议,我应该如何触发'点击'点击? – Stranger26

+0

您需要一个事件处理程序来设置每个节点的展开/折叠状态。您可能需要查看某种JSON资源管理器包,让您深入了解树的分支。 –

+0

好的,再次感谢你!将研究它。 – Stranger26