【问题标题】:Break in g:if / g:each中断 g:if / g:each
【发布时间】:2015-06-15 21:18:27
【问题描述】:

我在 gsp 中有这样的循环:

<g:each in="${personInstance.followed}" var="c" >
            <g:if test="${c.equals(person)}">
            <g:link id="${person.id}" action="unfollow" controller="message">unfollow</g:link>
            </g:if>
</g:each>

如何在 g:each 或 g:if 中使用 break? 有什么想法吗?

【问题讨论】:

  • g:ifg:each 的逻辑标签中没有break 的概念。你想达到什么目的?从您发布的代码中不清楚。
  • 如果第一次返回true,我需要打破这个循环。
  • 那么,您在personInstance.followed 属性中有一个persons 集合,如果该集合包含person,您需要显示一些内容?是对的吗?如果是这样,有更好的方法来解决这个问题(我可以在答案中解释)。
  • 如果人在此集合中,我需要显示某事,如果不在此集合中,则需要显示其他内容。

标签: html grails gsp


【解决方案1】:

听起来好像您希望根据实例是否在集合中来显示一件事或另一件事。您最好的选择是在集合上使用contains。例如:

<g:if test="${personInstance.followed.contains(person)}">
  Display your unfollow stuff here ...
</g:if>
<g:else>
  Display your follow stuff here ...
</g:else>

【讨论】:

    【解决方案2】:

    通常在 Groovy 和 GSP 中,您不需要 break,而您真正想要的是使用 findAll 标记来过滤条件,以便您的逻辑变为(未经测试的示例代码):

    <g:findAll in="${personInstance.followed}" expr="c.equals(person)" var="c" >
            <g:link id="${person.id}" action="unfollow" controller="message">unfollow</g:link>
    </g:findAll>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多