iterating nth numbers and stat variables properties

classic Classic list List threaded Threaded
3 messages Options
Nim
Reply | Threaded
Open this post in threaded view
|

iterating nth numbers and stat variables properties

Nim
Hey again,

I wanted to iterate a list of objects and then insert a spacer div after 5 objects. However I get an EL error.

Here is my code:
<div th:each="child:${children}" th:unless="${children==null}">
     <div class="span4">
          <button class="btn" type="button">
                 <strong>
                       <div th:text="${child.name}">John Doe</div>
                       <div th:text="${child.number}">12345</div>
                  </strong>
           </button>
      </div>
</div>
<div class="div-spacer" th:if="${childStat.count%5==0}">&nbsp;</div>

So basically I wanted to put 5 divs back to back then put a spacer and resume iterating. However, I am getting this error:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 10): Field or property 'count' cannot be found on null

I debugged just to see if the list was coming in null (despite the fact that there is a th:unless there checking for it) and it's not.

Any help is appreciated.

Edit: I have also tried
th:each="child,childStat:${children}"
But the tutorial states that the status variable is made automatically. Just saying in case people think its due it not being initialized or something.
Reply | Threaded
Open this post in threaded view
|

Re: iterating nth numbers and stat variables properties

Emanuel
Administrator
I think the error is because childStat is out of scope.  Iteration variables are like th:with/th:object, in that they are scoped to the tag that they're declared on.

The solution might then be to include the spacer div within the loop:

<div th:each="child, childStat: ${children}">
  ...
  <div th:if="${childStat.count % 5 == 0}">&nbsp;</div>
</div>
Nim
Reply | Threaded
Open this post in threaded view
|

Re: iterating nth numbers and stat variables properties

Nim
You are correct, thank you.

Funny because I realized something was shady about the div hierarchy shortly before you said so.

Either way thank you.

For those looking for spacers, in between nth amount of iterations - the modulus method works so make sure you try it if you're looking for that!