使用th:each限制显示的项目数

时间:2014-12-26 20:11:04

标签: java spring spring-boot thymeleaf

在下面的百万富翁代码中,我想将列表produtos中显示的项目数限制为特定数字:

  <div class="col-xs-6 col-lg-4" th:each="item2 : ${produtos}" th:if="${item.getId() == item2.getCategoria().getId()}">
    <h2 th:text="${item2.getNome()}"></h2>
    <div class="row">
      <div class="col-xs-6 col-md-3">
        <a th:href="@{/produto/__${item2.getId()}__.htm}" class="thumbnail">
          <img th:src="@{/Produto/icone__${item2.getId()}__.jpeg}" width="64" height="64" th:alt="${item2.getNome()}"/>
        </a>
      </div>
    </div>
    <p th:text="${#strings.substring(item2.getDescricao(), 0, 140)}"></p>
    <p><a class="btn btn-default" th:href="@{/produto/__${item2.getId()}__.htm}" role="button">Detalhes &raquo;</a></p>
  </div>

但在官方文档中,我无法找到任何说明如何操作的内容。任何人都可以给出任何暗示吗?

更新

我尝试解决此问题,将其添加到我的代码中:

th:if="${itemStat.index < NUM}"

最终的代码是:

<div class="col-xs-6 col-lg-4" th:each="item2 : ${produtos}" th:if="${item2Stat.index < NUM}" th:if="${item.getId() == item2.getCategoria().getId()}">

但是我收到了这个错误:

org.xml.sax.SAXParseException: the value of the attribute "th:if" associated to an element "null" shouldn't have the character '<'.

2 个答案:

答案 0 :(得分:1)

您可以在th:每个属性中使用第二个参数,例如:

<div class="col-xs-6 col-lg-4" th:each="item2, stat : ${produtos}" th:if="${item.getId() == item2.getCategoria().getId()}">

然后您可以访问此属性中的其他数据。这里有一个完整的解释: http://www.thymeleaf.org/doc/usingthymeleaf.html#iteration 在主题6.2下

答案 1 :(得分:0)

我设法解决了这个问题,将其添加到我的代码中:

th:each="item2 : ${#numbers.sequence(1,3)}"
相关问题