如何在Thymeleaf中迭代X项?

Dra*_*tis 7 thymeleaf

我有模板,对于四个li元素,我应该有一个ul元素.我该怎么做?现在我有类似的东西:

<div th:each="excursion,iterStat : ${excursions}">
    <ul th:if="${iterStat.index}/4 == 0">
        <li>
            <a th:href="@{/excursion/{id}(id=${excursion.excursionId})}"><img src="/template/images/garden1.jpg" alt="Image" /></a>
            <h2 th:text="${excursion.title}"></h2>
            <p th:text="${#strings.abbreviate(excursion.description,128)}"></p>
        </li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

我认为if条件将应用于elvery ul元素,但它隐藏了所有东西,包括li元素.

Aes*_*eir 9

根据您的评论,您需要一个包含4个项目的列表,下面将介绍.如果你遇到问题,请告诉我

<ul>
<div th:each="excursion,iterStat : ${excursions}" th:if="${iterStat.index}<5">    
        <li>
            <a th:href="@{/excursion/{id}(id=${excursion.excursionId})}"><img src="/template/images/garden1.jpg" alt="Image" /></a>
            <h2 th:text="${excursion.title}"></h2>
            <p th:text="${#strings.abbreviate(excursion.description,128)}"></p>
        </li>    
</div>
</ul>
Run Code Online (Sandbox Code Playgroud)

编辑1:根据提供的数据进一步审查产生另一种可能性.在传递之前使用Map而不是Controller中的质量列表:

Map<String, List<Excursion>> excursionsList;
Run Code Online (Sandbox Code Playgroud)

确保将每次偏移限制为4(根据需要).然后在Thymeleaf迭代地图.

<div th:each="excursion,rowStat : *{excursionsList}">
<ul>
<div th:each="list,iterStat : *{excursion[__${rowStat.index}__].value}">
//your code for each list item information such as excursionId, description etc.
</div>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)

这应该清理你的代码堆并根据需要进行编写.


归档时间:

查看次数:

17825 次

最近记录:

7 年,6 月 前