-1

everybody,

I have a problem with Thymeleaf, I tried to set the variable "iterator" in my for: each.

By default "iterator=0;" but if the condition is true I set the "iterator+1" but my variable is always 0 even the condition is true.

I need this "iterator" for my next block condition because if the iterator ==1 i need to something show else in my view

Here you can see my code:

 <div class="col-2"  th:each="t:${horaire.terrains}">
    <h2 th:text="${t.nomTerrain}"></h2>
    <table border="1px"  >

        <tr  th:each="h,index : ${heures.subList(0,15)}"   >

            <td>

       <th:block th:each="resa:${reservations}"  
   th:if="${resa.dateReservation eq date && resa.heureDebut eq h}">


               <a>reserve</a>

           <p th:with="iterator=${iterator+1} "></p>
           <!--<p th:text="${iterator}"></p>-->

       </th:block>

                <th:block th:if="${iterator eq 0 }">



                    <a th:text="${h}" th:href="@{addResa(heure=${h} , 
                   terrain=${t.id} ,date=${date},date2=${index.index+1}, 
                listeHeures = ${heures} )}"></a>
                </th:block>


            </td>
        </tr>

        <!--<a th:text="${h}" th:href="@{addResa(heure=${h} , terrain=${t.id} 
        ,date=${date},date2=${index.index+1}, listeHeures = ${heures} )}"> 
       </a>-->


    </table>
</div>

Here, in my controller, you can see the variable iterator:

    int iterator=0;

    model.addAttribute("iterator",iterator);
marian mereuta
  • 93
  • 2
  • 10
  • This isn't possible. You can't assign and change variables like that in thymeleaf. In general, they are read only. – Metroids Jul 09 '18 at 14:39

1 Answers1

0

What is the need to set iterator variable value in controller model.

Why cant you use below.

Thymeleaf th:each allows you to declare an iteration status variable

Then in the loop you can refer to iter.index and iter.size.

See http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

Alien
  • 15,141
  • 6
  • 37
  • 57