Expand my Community achievements bar.

SOLVED

How to process the last item in findResources

Avatar

Former Community Member

I am trying to process the LAST item in a findResources query differently from all others, but I can't fugure out how to do that. Here is a code snippet that I'm trying out (findResources returns THREE 'telephone' objects in my JCR) :-

<sling:findResources query="/jcr:root ${currentPage.path} //*[${tagCriteria} @sling:resourceType = 'avivapocs/components/telephone']" language="xpath" var="resources" /> <c:forEach var="telephoneResource" items="${resources}"> <c:choose>             <c:when test="${resources}.hasNext()">                 DO SOMETHING WITH ALL OTHER telephone resource ITEMS             </c:when>             <c:otherwise>                 DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM             </c:otherwise>         </c:choose> </c:forEach>

But the 'otherwise' path is chosen in ALL cases.

Anyway, I am a bit stumped .. so any advice would be welcome.

Regards

Fraser.

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi,

The correct way to do this with the c:forEach tag is to use the varStatus attribute to capture the status object (of type http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/c...). It would look something like this:

 

<c:forEach var="telephoneResource" items="${resources}" varStatus="status"> <c:choose> <c:when test="${status.last}"> DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM </c:when> <c:otherwise> DO SOMETHING WITH ALL OTHER telephone resource ITEMS </c:otherwise> </c:choose> </c:forEach>

Regards,

Justin

View solution in original post

2 Replies

Avatar

Correct answer by
Employee

Hi,

The correct way to do this with the c:forEach tag is to use the varStatus attribute to capture the status object (of type http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/c...). It would look something like this:

 

<c:forEach var="telephoneResource" items="${resources}" varStatus="status"> <c:choose> <c:when test="${status.last}"> DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM </c:when> <c:otherwise> DO SOMETHING WITH ALL OTHER telephone resource ITEMS </c:otherwise> </c:choose> </c:forEach>

Regards,

Justin

Avatar

Former Community Member

justin_at_adobe wrote...

Hi,

The correct way to do this with the c:forEach tag is to use the varStatus attribute to capture the status object (of type http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/c...). It would look something like this:

 

  1. <c:forEach var="telephoneResource" items="${resources}" varStatus="status">
  2. <c:choose>
  3. <c:when test="${status.last}">
  4. DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM
  5. </c:when>
  6. <c:otherwise>
  7. DO SOMETHING WITH ALL OTHER telephone resource ITEMS
  8. </c:otherwise>
  9. </c:choose>
  10. </c:forEach>

Regards,

Justin

 

Justin,

spot on as usual. I had used the 'count' method of the varStatus variable in other circumstances, but hadn't realised that there were others like 'last'. Also count implies you have access to the number of items in the collection (perhaps using a 'length' function) but findResources returns an Iterator so that's not possible. You can use negation via the 'not' operator. In my case I was able to simply the logic, replacing the <c:choose> with an <c:if> as in :-

<c:if test="${not status.last}">do something with the last item</c:if>

Thanks for the pointer

Fraser