5

I have a 2D array in a JSON object (called table ;)

data = {

tableID : "testTable",

table : [
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}],
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}],
[{type:'a', value:'x'},{type:'a', value:'y'},{type:'a', value:'z'}]
]

};

And have been successfully rendering it out with handlebars using the template:

<table id = "{{tableID}}-table">

{{#each table}}

    <tr id = "{{../tableID}}-row-{{@index}}">

        {{#each this}}

            <td id = "{{../../tableID}}-row-{{../index}}-col-{{@index}}">

                {{this.type}}-{{this.value}}

            </td>

        {{/each}}

    </tr>

{{/each}}

</table>

However, in the td tag id I can't seem to access the parent index {{../index}} - the index of the row. Nothing is returned:

<td id = "testTable-row--col-x">

However, I can access the index of the current context {{@index}}.

Any ideas??

Many many thanks in advance!!

Rich

p.s. Using 1.0.0-rc.3

  • Yes, that's a good point. It's the direction I've adopted at the moment, but was hoping for something a bit cleaner ;) – user2212212 Apr 02 '13 at 10:46
  • possible duplicate of [Handlebars.js: How to access parent index in nested each?](http://stackoverflow.com/questions/14854491/handlebars-js-how-to-access-parent-index-in-nested-each) – gius Nov 11 '14 at 17:54

2 Answers2

3

Since Handlebars version 2.0.0, you can use

{{@../index}}
gius
  • 9,289
  • 3
  • 33
  • 62
2

This is an open issue/feature on handlebar. You can check the progress for the same here

However you can check the workaround here

Community
  • 1
  • 1
Vinayak Sakhare
  • 768
  • 1
  • 7
  • 14