thymeleaf 显示 table 的前 k 行

thymeleaf show top-k rows of a table

我有一个 table 和一个多行数据集。我不想跨越此 table 的所有行。所以我创建了一个计数器,但在 if 条件下出现错误:

<div th:if="${dataset}" th:with="counter=0">

    <table class="table">
        <tbody>
        <th:block th:each="t_log : ${dataset.rows}" th:with="counter=${counter} + 1">

            <tr th:if="${counter <= 5 }">
                <td th:text="${t_log.title}"/>
                <td th:if="${t_log.msg == '1'}" th:text="Online"/>
                <td th:if="${t_log.msg == '0'}" th:text="Offline"/>
            </tr>
        </th:block>
        </tbody>
    </table>
</div>

我在这里看到了一个例子:

http://forum.thymeleaf.org/Displaying-an-iterable-of-n-items-in-rows-of-3-items-with-thymeleaf-td4025738.html

但是我的计数器没有用。

试试这个 Thymeleaf 有一个内置计数 属性。请参阅文档的 6.2 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html 以及结帐部分 4.9 您可能需要将 <= 更改为 le;

<div th:if="${dataset}">

    <table class="table">
        <tbody>
        <th:block th:each="t_log,count : ${dataset.rows}">

            <tr th:if="${count <= 5 }">
                <td th:text="${t_log.title}"/>
                <td th:if="${t_log.msg == '1'}" th:text="Online"/>
                <td th:if="${t_log.msg == '0'}" th:text="Offline"/>
            </tr>
        </th:block>
        </tbody>
    </table>
</div>