thymeleaf show top-k rows of a table

和自甴很熟 提交于 2021-01-28 05:44:25

问题


I have a table and a multiline dataset. I do not want to span all the rows of this table. So I created a counter but I get an error at the if condition:

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

I have seen an example from here:

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

But my counter does not do the trick.


回答1:


Try this Thymeleaf has a built in count property. See 6.2 of the documentation http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html also checkout section 4.9 you may need to change <= to 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>


来源:https://stackoverflow.com/questions/29141584/thymeleaf-show-top-k-rows-of-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!