2015-03-19 71 views
1

我有一张表和一个多行数据集。我不想跨越此表的所有行。所以,我创建了一个柜台,但我在,如果条件得到一个错误:百里香显示表格的顶部k行

<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

但我的计数器不会做的伎俩。

回答

1

试试这个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> 
+0

是因为我看到,如果你不添加额外的iterstat,thymeleaf对变量t_log,t_logStat.count隐藏状态做这项工作。 – bill 2015-03-20 09:33:12

+0

我实际上更喜欢使用明确的Stat变量:th:each =“t_log,t_logStat:$ {dataset.rows}”和th:if =“$ {t_logStat.count} le 5”。 t_logStat有各种有用的变量,count(从1开始),index(从0开始)等等。 – 2016-08-02 17:19:14