Access parent item inside a loop in jsrender
Asked Answered
F

1

7
<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 **.

Fixed answered 3/1, 2013 at 10:18 Comment(2)
If i give {{:#parent.parent.data.LocationId}} inside option tag i am able to see value of it. But i want to consume the same in if statement.Fixed
Location is the child array which has the values to create dropdown.Fixed
A
13

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

Animalist answered 4/1, 2013 at 8:17 Comment(2)
Thanks for replying. How do I use this in if statement?Fixed
{{if LocationId == ~locationId }}Animalist

© 2022 - 2024 — McMap. All rights reserved.