Home > OS >  I want to do indexed loop for each th:each loop
I want to do indexed loop for each th:each loop

Time:08-02

for example,

<div  th:each="price, priceStat : ${itemPrice0}" th:if="${priceStat.index<1}">
            <span><a>$</a><a th:text="${price.normal}"></a></span>
</div>

i want to repeat it for itemPrice0, itemPrice1, itemPrice2, ... then there will be loop for

<div  th:each="price, priceStat : ${itemPrice0}" th:if="${priceStat.index<1}">
            <span><a>$</a><a th:text="${price.normal}"></a></span>
</div>
<div  th:each="price, priceStat : ${itemPrice1}" th:if="${priceStat.index<1}">
            <span><a>$</a><a th:text="${price.normal}"></a></span>
</div>
<div  th:each="price, priceStat : ${itemPrice2}" th:if="${priceStat.index<1}">
            <span><a>$</a><a th:text="${price.normal}"></a></span>
</div>

is there any way to do it in this way?

CodePudding user response:

Yes, you can do this with preprocessing.

<th:block th:each="i: ${#numbers.sequence(0, 2)}">
  <div  th:each="price, priceStat : ${itemPrice__${i}__}" th:if="${priceStat.index<1}">
    <span><a>$</a><a th:text="${price.normal}"></a></span>
  </div>
</th:block>

(However, this is bad design. Instead, I would recommend adding a List<List<ItemPrice>> to the model instead -- and looping over both lists.)

  • Related