Grails - testing the first item in a set using gsp each

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

Here is 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 that you can check the status in g: each operator, but only an integer. Is there anything to access the first and last item? If not, is there any other way to do what I do?

Any help was appreciated.

+3
source share
2 answers

I am not sure what the problem is with <g:each status>. Yes, it is a "just" integer, but isn't that what you need?

<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>
+7
source

, - Groovy List first(). , Groovy List last().

list.First()

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

list.last()

<g:forEach items='${aSet}' var='elem' varStatus="loop">
   <g:if test='${elem == aSet.last()}'>
      <p>Display something for last element</p>
   </g:if>
</g:forEach>
+2

Source: https://habr.com/ru/post/1721277/


All Articles