![](/img/trans.png)
[英]Tracking even/odd index of Meteor Spacebars Collection (blaze)
[英]Multiple each index on Meteor Blaze
我正在使用Meteor的Blaze创建html模板,并且有多个循环,如:
let objects = [{name: 'John', age: 18}, {name: 'Foo', age: 25}, {name: 'Bar', age: 35}]
let attrs = ['name', 'age']
{{#each objects}}
<h3>Object {{@index}}</h3>
{{#each attrs}}
[...] // code here
{{/each}}
{{/each}}
我知道 :
{{@index}}
用于了解当前循环索引(因此[..]
{{@index}}
是对attrs数组索引的引用 {{this}}
用于了解当前循环值(因此[...]
{{value}}
是name
或age
) {{..}}
是对父循环值的引用(因此,在[...]
{{..}}
是第一个循环上的当前对象) 现在,我想在[...]
针对当前索引 objects
循环。 我在Google和Stackoverflow上搜索了很多,但没有找到。
Huumm,有关Blaze的新闻允许new创建变量(使用#let)。 一个简单的解决方案是:
let objects = [{name: 'John', age: 18}, {name: 'Foo', age: 25}, {name: 'Bar', age: 35}]
let attrs = ['name', 'age']
{{#each objects}}
{{#let object=this object_idx=@index}}
<h3>Object {{object_idx}}</h3>
{{#each attrs}}
Object idx {{object_idx}} | Object {{object}}
Attr idx {{@index}} | Attr {{this}}
{{/each}}
{{/let}}
{{/each}}
更新:你甚至可以做{{#each object in objects}}
因为一段时间,避免你做{{#let object=this}}
{{#each object in objects}}
{{#let object_idx=@index}}
<h3>Object {{object_idx}}</h3>
{{#each attr in attrs}}
Object idx {{object_idx}} | Object {{object}}
Attr idx {{@index}} | Attr {{attr}}
{{/each}}
{{/let}}
{{/each}}
您需要为内循环创建一个帮助器,如下所示:
Template.myTemplate.helpers({
value: function(){
return Template.parentData(1)[this];
}
});
Template.parentData(1)
返回比当前级别高一(1)级别的数据上下文。 [this]
引用当前数据给定的该对象的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.