gpt4 book ai didi

thymeleaf - 如何在 Thymeleaf 中迭代 X 个项目?

转载 作者:行者123 更新时间:2023-12-04 11:05:10 25 4
gpt4 key购买 nike

我有模板,对于四个 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 条件将应用于 elvery ul 元素,但它隐藏了所有内容,包括 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:
基于提供的数据的进一步审查产生了另一种可能性。
在传递之前在 Controller 中使用 Map 而不是大量列表:
Map<String, List<Excursion>> excursionsList;

确保将每次游览限制为 4 次(根据需要)。
然后在 Thymeleaf 中遍历 map 。
<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>

这应该清理您的代码堆并根据需要进行处理。

关于thymeleaf - 如何在 Thymeleaf 中迭代 X 个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27095444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com