Thymeleaf - How to loop a list by index

前端 未结 2 1072
礼貌的吻别
礼貌的吻别 2020-12-08 09:25

How can I loop by index?

Foo.java

public Foo {
    private List tasks;
    ...
}

index.html



        
相关标签:
2条回答
  • 2020-12-08 10:16

    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

    0 讨论(0)
  • 2020-12-08 10:19

    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.

    0 讨论(0)
提交回复
热议问题