JsRender如果条件比较子对象之间的变量 [英] JsRender If conditions comparing variables between child objects

查看:281
本文介绍了JsRender如果条件比较子对象之间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在JsRender中实现这一点。
正如你从下面的代码中看到的,SubMenuItems和SubMenuPages是一个对象数组中的子对象。但是,我想比较第一个Object中的值和第二个中的值。我只有合格if条件就是这么清楚我想完成什么。



所以基本上我想在一个子对象中访问一个id变量,然后检查另一个子对象在另一个对象中变量。
我对这个语法有点不确定,因为我试过的所有东西都没有返回。

  {{ for subMenuItems}} 
< li>< a href =#> {{> Name}}< / a>< / li>
< ul>
{{SubMenuPages}}

{{如果SubMenuItems.PageMenuId == SubMenuPages.Id}}

< li>< a href ={ {> PageUrl}}> {{PageName}}< / a>< / li>

$ b $ {{if}}


{{/ for}}
{{/ for}}
< / UL>


解决方案

因此, SubMenuItems 一个 数组 ,其中每个项目都有一个 PageMenuId - 或者它是一个单独的对象你好像说? (同样, SubMenuPages array 的项目都有一个 Id ?)



如果没有更清晰的数据层次来回答您的问题是很困难的,但基本上这里的文档查看层次结构 - 特别是这里访问父数据应该告诉你你需要知道什么。



如果我对你的数据结构的猜测是正确的,那么这里是您可以编写模板的一种方式:

  ... 
{{用于SubMenuItems itemVar =〜SubMenuItem 〜Pages = SubMenuPages}}
...
{{for〜Pages itemVar =〜SubMenuPage}}
...
{{if〜SubMenuItem.PageMenuId = =〜SubMenuPage.Id}}
...
{{/ if}}
...
{{/ for}}
...
{{/ for}}
...

Y你也可以看看这些类似于你的堆栈溢出问题:



JsRender。由顶部循环的对象嵌套循环


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>

解决方案

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

这篇关于JsRender如果条件比较子对象之间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆