如何在百里叶中迭代X个项目? [英] How to iterate for X items in Thymeleaf?

查看:9
本文介绍了如何在百里叶中迭代X个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板,对于四个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>

我以为if条件将应用于Elveryul元素,但它隐藏了所有内容,包括li元素。

推荐答案

根据您的评论,您需要一个包含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>
编辑1: 基于所提供数据的进一步审查产生了另一种可能性。 在传递之前,请在控制器中使用贴图而不是质量列表:

Map<String, List<Excursion>> excursionsList;

确保将每次旅行限制在4次(根据您的需要)。 然后在百里叶中迭代映射。

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

这应该会清理您的代码堆,并在您需要时生成它。

这篇关于如何在百里叶中迭代X个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆