Reputation: 148
I am looking to use a for loop in my ThymeLeaf view.
I have a list model holding 8 objects.
I would like to loop objects though 1 to 4 in one area and from 5 to 8 in another.
Can this be done?
Is there a way of manipulating a for each loop to suit my application?
Thanks in advance!
Upvotes: 0
Views: 760
Reputation: 20477
In this case, you should probably use the #numbers utility object. Something like this, for example:
<span th:each="n: ${#numbers.sequence(1, 4)}" th:text="${n}" />
<span th:each="n: ${#numbers.sequence(5, 8)}" th:text="${n}" />
(These two blocks will print 1 through 4 and 5 through 8, respectively.) Your list (if named list), would look like this:
<span th:each="n: ${#numbers.sequence(1, 4)}" th:text="${list[n].fieldOne}" />
<span th:each="n: ${#numbers.sequence(5, 8)}" th:text="${list[n].fieldTwo}" />
Upvotes: 2
Reputation: 4451
Look here http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status
You can access the actual index and count variables using the th:each
From there on you can read the value in the first block and stop when your count is 4. And in the second block you can add another th:each
iterating again but only from count 5 on.
Upvotes: 3