292

我在我的项目中使用 Handlebars 进行模板化。有没有办法获取 Handlebars 中“每个”助手的当前迭代的索引?

<tbody>
     {{#each item}}
         <tr>
            <td><!--HOW TO GET ARRAY INDEX HERE?--></td>
            <td>{{this.key}}</td>
            <td>{{this.value}}</td>
         </tr>
     {{/each}}
</tbody>
4

7 回答 7

555

在较新版本的 Handlebars 索引(或对象迭代情况下的键)中,默认情况下每个助手都提供标准。


摘自:https ://github.com/wycats/handlebars.js/issues/250#issuecomment-9514811

当前数组项的索引已经通过@index 提供了一段时间:

{{#each array}}
    {{@index}}: {{this}}
{{/each}}

对于对象迭代,请改用@key:

{{#each object}}
    {{@key}}: {{this}}
{{/each}} 
于 2013-01-01T19:56:28.190 回答
18

这在较新版本的 Ember 中发生了变化。

对于数组:

{{#each array}}
    {{_view.contentIndex}}: {{this}}
{{/each}}

看起来#each 块不再适用于对象。我的建议是为它滚动你自己的辅助函数。

感谢这个提示

于 2014-07-09T21:12:20.613 回答
16

In handlebar version 3.0 onwards,

{{#each users as |user userId|}}
  Id: {{userId}} Name: {{user.name}}
{{/each}}

In this particular example, user will have the same value as the current context and userId will have the index value for the iteration. Refer - http://handlebarsjs.com/block_helpers.html in block helpers section

于 2017-05-20T16:43:43.597 回答
14

I know this is too late. But i solved this issue with following Code:

Java Script:

Handlebars.registerHelper('eachData', function(context, options) {
      var fn = options.fn, inverse = options.inverse, ctx;
      var ret = "";

      if(context && context.length > 0) {
        for(var i=0, j=context.length; i<j; i++) {
          ctx = Object.create(context[i]);
          ctx.index = i;
          ret = ret + fn(ctx);
        }
      } else {
        ret = inverse(this);
      }
      return ret;
}); 

HTML:

{{#eachData items}}
 {{index}} // You got here start with 0 index
{{/eachData}}

if you want start your index with 1 you should do following code:

Javascript:

Handlebars.registerHelper('eachData', function(context, options) {
      var fn = options.fn, inverse = options.inverse, ctx;
      var ret = "";

      if(context && context.length > 0) {
        for(var i=0, j=context.length; i<j; i++) {
          ctx = Object.create(context[i]);
          ctx.index = i;
          ret = ret + fn(ctx);
        }
      } else {
        ret = inverse(this);
      }
      return ret;
    }); 

Handlebars.registerHelper("math", function(lvalue, operator, rvalue, options) {
    lvalue = parseFloat(lvalue);
    rvalue = parseFloat(rvalue);

    return {
        "+": lvalue + rvalue
    }[operator];
});

HTML:

{{#eachData items}}
     {{math index "+" 1}} // You got here start with 1 index
 {{/eachData}}

Thanks.

于 2015-05-08T11:29:11.160 回答
8

Arrays:

{{#each array}}
    {{@index}}: {{this}}
{{/each}}

If you have arrays of objects... you can iterate through the children:

{{#each array}}
    //each this = { key: value, key: value, ...}
    {{#each this}}
        //each key=@key and value=this of child object 
        {{@key}}: {{this}}
        //Or get index number of parent array looping
        {{@../index}}
    {{/each}}
{{/each}}

Objects:

{{#each object}}
    {{@key}}: {{this}}
{{/each}} 

If you have nested objects you can access the key of parent object with {{@../key}}

于 2017-05-22T08:31:01.037 回答
2

Using loop in hbs little bit complex

<tbody>
     {{#each item}}
         <tr>
            <td><!--HOW TO GET ARRAY INDEX HERE?--></td>
            <td>{{@index}}</td>
            <td>{{this}}</td>
         </tr>
     {{/each}}
</tbody>

Learn more

于 2020-10-17T19:57:30.590 回答
0

In handlebar version 4.0 onwards,

{{#list array}}
  {{@index}} 
{{/list}}
于 2020-05-23T04:29:25.837 回答