Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars.js - Accessing parent index from 2D array

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

like image 396
user2212212 Avatar asked Oct 05 '22 14:10

user2212212


2 Answers

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

However you can check the workaround here

like image 68
Vinayak Sakhare Avatar answered Oct 13 '22 11:10

Vinayak Sakhare


Since Handlebars version 2.0.0, you can use

{{@../index}}
like image 35
gius Avatar answered Oct 13 '22 11:10

gius