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