Handlebars.js: How to access parent index in nested each?

后端 未结 5 1333
情歌与酒
情歌与酒 2020-12-12 20:57

How to access parent @index value in each-loop?

Tried the following:

{{#each company}}
{{#each employee}}
  {{../@index}} // how to access company in         


        
5条回答
  •  清歌不尽
    2020-12-12 21:14

    registe an Helper method:

    Handlebars.registerHelper('eachWithIndex', function(cursor, options) {
        var fn = options.fn, inverse = options.inverse;
        var ret = "";
        var idx = -1;
        //console.log(cursor);
        cursor.forEach(function(item){
            idx++;
            console.log(item.index);
            item.index = idx;
            ret+=fn(item);
        });
        return ret;
    }); 
    

    handlebars template:

    {{#eachWithIndex outer}}
      {{#each inner}}
       {{../index}} // access outer index like this. I used hanlebars V1.3.0
       {{index}} // access inner index
      {{/each}}
    {{/eachWithIndex}}
    

提交回复
热议问题