How can I loop by index?
Foo.java
public Foo {
private List tasks;
...
}
index.html
Thymeleaf always declares implicit iteration status variable if we omit it.
<span th:each="task : ${foo.tasks}">
<span th:text="${taskStat.index} + ': ' + ${task.name}"></span>
</span>
Here, the status variable name is taskStat
which is the aggregation of variable task
and the suffix Stat
.
Then in the loop, we can refer to taskStat.index
, taskStat.size
, taskStat.count
, taskStat.even
and taskStat.odd
, taskStat.first
and taskStat.last
.
Source: Tutorial: Using Thymeleaf - 6.2 Keeping iteration status
Thymeleaf th:each
allows you to declare an iteration status variable
<span th:each="task,iter : ${foo.tasks}">
Then in the loop you can refer to iter.index
and iter.size
.
See Tutorial: Using Thymeleaf - 6.2 Keeping iteration status.