问题
I am trying to achieve this in JsRender. As You can see from the code below, SubMenuItems and SubMenuPages are child objects in a array of objects.However, I would like to compare a value in the first Object with a value in the second respectively.I have only qualified the if condition just so it is clear what I'm trying to accomplish.
So basically I would like to access an id variable in one child object and then check against another variable in another object. I am a bit unsure as to the syntax regarding this , since all i tried kept returning nothing.
{{for SubMenuItems}}
<li><a href="#">{{>Name}}</a></li>
<ul>
{{for SubMenuPages}}
{{if SubMenuItems.PageMenuId == SubMenuPages.Id }}
<li><a href="{{>PageUrl}}" >{{PageName}}</a></li>
{{/if}}
{{/for}}
{{/for}}
</ul>
回答1:
So is SubMenuItems
an array of items of which each has a PageMenuId
- or is it a single object as you seem to say? (And similarly, is SubMenuPages
an array of items each of which has an Id
?)
It is difficult to answer your question without more clarity on your data hierarchy, but basically the documentation here View Hierarchy - and in particular here Accessing parent data should tell you what you need to know.
If my guess about your data structure is correct, then here is one way you can write your template:
...
{{for SubMenuItems itemVar="~SubMenuItem" ~Pages=SubMenuPages}}
...
{{for ~Pages itemVar="~SubMenuPage"}}
...
{{if ~SubMenuItem.PageMenuId == ~SubMenuPage.Id }}
...
{{/if}}
...
{{/for}}
...
{{/for}}
...
You could also look at these stack overflow questions which are similar to yours:
Access parent variable from nested block in JsRender
JsRender. Nested loop by the object of top loop
来源:https://stackoverflow.com/questions/35119731/jsrender-if-conditions-comparing-variables-between-child-objects