我尝试在百里香中使用th:remove =“all-but-first”但它没有用

时间:2015-07-05 13:06:05

标签: thymeleaf

<td th:remove="all-but-first"> 
     <span th:each="groups : ${userView.user.groupMemberships}">
         <span th:text="${groups.group.name}"></span>;
    </span>
</td>

我试图显示th:each中返回的前两行,所以我尝试首先显示第一行但它不起作用。

如果我们只想显示th:each中的前两行或哪三行,我们如何只显示第一行以及我们应该怎么做?

2 个答案:

答案 0 :(得分:1)

属性th:remove用于删除原型生成的标签。

举个例子:

<table th:remove="all-but-first">
    <tr th:each="user : ${users}">
        <td th:text="${user.name}">John Apricot</td>
    </tr>
    <tr>
        <td>Martha Apple</td>
    </tr>
    <tr>
        <td>Frederic Orange</td>
    </tr>
</table>

这意味着将删除第二个和第三个tr,只留下用于迭代的tr。更多详情here

因此,如果您只想显示集合的第一个元素,则无需执行迭代,您只需通过索引访问它(如果是地图,甚至是键)。

示例:

<td> 
    <span th:text="${userView.user.groupMemberships[0].group.name}"></span>
</td>

答案 1 :(得分:0)

显示我的集合的前两个元素,然后给出了更多选项以查看其余元素

<td> <span th:each="groups,itrStatus : ${userView.user.groupMemberships}"> <span th:if="${itrStatus.count < 3}">
<span th:if="${itrStatus.count == 2}">,</span> <span th:text="${groups.group.name}"></span> </span> <span th:if="${itrStatus.count == 3}"> , <a href="#" th:attr="data-value=${userView.user.userId}" class="show-user-groups" data-toggle="modal" data-title="Add new user" data-target="#show-user-groups-modal">See more</a> </span> </span> </td>

相关问题