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

Javascripthandlebars.js

Javascript Problem Overview


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

Tried the following:

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

This results to an error: > Expecting 'ID', got 'DATA'

Javascript Solutions


Solution 1 - Javascript

There is a syntax error in the example. The correct syntax is {{@../index}}.

We are looking at ways that we can support custom naming of these parameters in future versions of the language so this is easier to deal with. https://github.com/wycats/handlebars.js/issues/907

Solution 2 - Javascript

This worked for me:

{{#each company}}
{{setIndex @index}}
{{#each employee}}
  {{../index}}
{{/each}}
{{/each}}

JS:

Handlebars.registerHelper('setIndex', function(value){
	this.index = Number(value + 1); //I needed human readable index, not zero based
});

Just make sure the company object doesn't have index property.

Solution 3 - Javascript

Answer: {{@../index}}

From the Handlebars docs (see bottom of 'each' helper section):

"Nested each blocks may access the interation variables via depted paths. To access the parent index, for example, {{@../index}} can be used."

NOTE: I'm using v1.3 so it's at least that old.

REMINDER: Helpers are your last best option. 9/10 there is a better solution.

Solution 4 - Javascript

It looks like there's a new syntax in Ember v2.2.0. I tried all the answers here and they didn't work for me.

What I found worked was naming the parent loop's index and the child loop's index.

{{#each parents as |parent parentIndex|}}
    {{parentIndex}}
    {{#each children as |child childIndex|}}
        {{parentIndex}}
        {{childIndex}}
    {{/each}}
{{/each}}

Solution 5 - Javascript

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}}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFdrView Question on Stackoverflow
Solution 1 - JavascriptKevin DeckerView Answer on Stackoverflow
Solution 2 - JavascriptstrahView Answer on Stackoverflow
Solution 3 - JavascriptjordanbView Answer on Stackoverflow
Solution 4 - JavascriptAndrew ToyView Answer on Stackoverflow
Solution 5 - JavascriptFishView Answer on Stackoverflow