如何设置限制:HTML中的每个循环

Pra*_*A R 2 html javascript each loops thymeleaf

如何eachthymeleafHTML页面中设置循环限制?

例如:

<select id="places" onchange="onPlaceChange();">
    <option value="0">Select Place</option>
    <option th:each="p:${places}" th:text="${p.place_Name}" th:value="${p.id}"></option>
</select>
Run Code Online (Sandbox Code Playgroud)

此代码循环遍历数据库中的项目,limit是列表的长度.

列表长度为14,我想将该限制设置为7,我该怎么做?

mar*_*111 6

关键是使用迭代状态的机制与沿th:ifth:unless条件属性.

相关参考文献位于:

所以在你的情况下,它看起来像是:

<option th:each="p,pStat : ${places}" th:text="${p.place_Name}" th:value="${p.id}" th:unless="${pStat.index > 7}"></option>
Run Code Online (Sandbox Code Playgroud)

编辑:这个答案是为Thymeleaf 2.1(当时)编写的,但应该以相同或相似的方式使用3.0.请参阅:https: //www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html