【问题标题】:set variable Thymeleaf in a for each for a next condition为每个下一个条件设置变量 Thymeleaf
【发布时间】:2018-12-16 09:44:28
【问题描述】:

大家,

我的 Thymeleaf 有问题,我尝试在我的 for: each 中设置变量“迭代器”。

默认情况下“iterator=0;”但如果条件为真,我设置“iterator+1”,但即使条件为真,我的变量也始终为 0。

我的下一个块条件需要这个“迭代器”,因为如果迭代器 ==1,我需要在我的视图中显示其他内容

在这里你可以看到我的代码:

 <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>

在这里,在我的控制器中,您可以看到变量迭代器:

    int iterator=0;

    model.addAttribute("iterator",iterator);

【问题讨论】:

  • 这是不可能的。您不能在 thymeleaf 中分配和更改类似的变量。通常,它们是只读的。

标签: spring-boot thymeleaf


【解决方案1】:

需要在控制器模型中设置迭代器变量值吗?

为什么不能在下面使用。

Thymeleaf th:each 允许你声明一个迭代状态变量

然后在循环中你可以参考iter.indexiter.size

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

【讨论】:

猜你喜欢
  • 2014-01-10
  • 2020-02-29
  • 2015-01-22
  • 2021-09-18
  • 2021-06-27
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多