Grails-使用gsp分别测试集合中的第一个元素 [英] Grails - Testing for the first element in a set using gsp each

查看:95
本文介绍了Grails-使用gsp分别测试集合中的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何测试gsp循环中的第一个成员和最后一个成员吗?

Does anyone know how to test for the first member and last member in a gsp loop?

这是我的jsp代码:

<c:forEach items='${aSet}' var='elem' varStatus="loop">
   <c:if test='${loop.first}'>
      <p>Display something</p>
   </c:if>
</c:forEach>

我知道您可以在g:each语句中测试状态,但这只是一个整数.有什么东西可以访问第一个和最后一个元素吗?如果没有,还有其他方法可以做我正在做的事情吗?

I know you can test for status in a g:each statement but that's just an integer. Is there anything to access the first and last element? If not, is there another way to do what I'm doing?

任何帮助表示赞赏.

推荐答案

我不确定<g:each status>的问题是什么.是的,它只是"一个整数,但这不正是您想要的吗?

I'm not sure what the problem with <g:each status> is. Yes, it's "just" an integer, but isn't that exactly what you want?

<g:each in="${aSet}" var="elem" status="i">
    <g:if test="${i == 0}">
        <p>Display something for first element</p>
    </g:if>
    <p>Stuff that is always displayed</p>
    <g:if test="${i == aSet.size() - 1}">
        <p>Display something for last element</p>
    </g:if>
</g:each>

这篇关于Grails-使用gsp分别测试集合中的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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