Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access parent item inside a loop in jsrender

Tags:

jsrender

<select id="Test" TestAttr="{{:LocationId}}">              
       {{for #parent.parent.data.Location}}                         
                  <option value="{{:LocationId}}" {{if LocationId= *#parent.parent.data.LocationId*}}selected{{/if}}>{{:#parent.parent.data.LocationId}}</option>                            
       {{/for}}
</select>

How to get the parent array's LocationId inside if statement which is mentioned in between **.

like image 787
Pradeep Avatar asked Jan 03 '13 10:01

Pradeep


1 Answers

You can introduce variable which will be visible inside loop (In official documentation: Setting contextual template parameters, accessible in all nested contexts as ~nameOfParameter)

<select id="Test" TestAttr="{{:LocationId}}">  
    {{for #parent.parent.data.Location ~locationId=LocationId}}
       <option value="{{:~locationId}}"...


EDITED POSTSCRIPT:

The above link is to previous documentation, now superceded.

For current documentation, see Accessing parent data.

See also this stackoverflow question

like image 125
Sergii Avatar answered Oct 04 '22 22:10

Sergii