Grails – Testing for the first element in a set using gsp each

grailsgsp

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

Here's my jsp code:

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

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?

Any help appreciated.

Best Answer

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>
Related Topic