JsRender If conditions comparing variables between child objects

雨燕双飞 提交于 2019-12-11 23:25:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!