【问题标题】:How to set limit th:each loop in HTML如何在 HTML 中设置限制 th:each 循环
【发布时间】:2014-06-08 02:11:17
【问题描述】:

如何在我的thymeleaf HTML 页面中为each 循环设置限制?

例如:

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

此代码循环访问数据库中的项目,limit 是列表的长度。

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

【问题讨论】:

    标签: javascript html loops each thymeleaf


    【解决方案1】:

    关键是使用迭代状态机制以及th:ifth:unless 条件属性。

    相关参考资料在:

    所以在你的情况下,它看起来类似于:

    <option th:each="p,pStat : ${places}" th:text="${p.place_Name}" th:value="${p.id}" th:unless="${pStat.index > 7}"></option>
    

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

    【讨论】:

    • 我应该怎么做才能将剩余的 7 个项目分别循环到该列表的末尾:每个?
    • @moonwalker:您可以在不同的
    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2021-02-07
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多