如何在流星模板中显示序列号

时间:2014-02-28 10:25:35

标签: meteor handlebars.js

我需要在流星应用中显示一个项目列表。 我的模板代码就像

 Template.templatename.vname = function(){
    return Groups.find();
 }


{{#each vname}}
// Need to display serial number here.
{{ field1 }}
{{/each}}

如何在此循环中显示序列号?

3 个答案:

答案 0 :(得分:1)

我找到了解决方案

Template.templatename.vname = function(){
   var gps_crs = Groups.find();
   var gps = gps_crs.fetch();
   for (var i = 0; i < gps.length; i++) {
        gps[i].index = i+1;
    }
    return gps;
 }


{{#each vname}}
{{ index }}
{{ field1 }}
{{/each}}

序列号将显示在{{ index }}

答案 1 :(得分:0)

<强>代码

Template.templatename.vname = function(){
    return Groups.find().fetch().map(function (d, i) {
        d._index = i + 1;
        return d;
    });
}

<强>模板

{{#each vname}}
  {{_index}}
  {{ field1 }}
{{/each}}

答案 2 :(得分:0)

如果我们使用

{{#each vname}}
{{@index }}
{{/each}} 

然后我们将得到一个从 0,1,2..... 开始的数字序列,从 1,2,3...得到数字...然后我们可以使用 Handlebar 数学助手并使用下面的代码...< /p>

{{#each vname}}
{{add @index 1}}
{{/each}}

或者我们自己编写一个迭代器辅助函数。