Thymeleaf - 表格数据表返回零记录时的建议方法(空列表)?

时间:2015-12-10 18:13:33

标签: thymeleaf

假设:

<table id="identification-data" class="pure-table">
<thead>
<tr>
  <th>Name</th>
   <th>DOB</th>
   <th>Gender</th>
 <tr>
 </thead>
 <tbody>
 <tr th:each="row : ${identificationData}">
   <td th:text="${row['Name']}">Brian Smith</td>
   <td th:text="${#calendars.format(row['Date of Birth'], 'MM/dd/yyyy')}">10/11/1971</td>
   <td th:text="${row['Gender']}">Male</td>
 </tr>
 </tbody>
 </table>

如果集合$ {identificationData}为空 - 是否有 thymeleafy 方式显示“未找到数据”这样的消息?

我可以在控制器端做一些事情,如:

if (identificationData.isEmpty()){
    model.addAttribute("identificationDataNotFound", Boolean.TRUE);
}
model.addAttribute("identificationData", identificationData);

1 个答案:

答案 0 :(得分:1)

最多&#34; thymeleafy&#34;我能想到的方法是有条件地渲染一个<tbody>,其中包含&#34;没有找到数据&#34;如果列表为空,则显示消息。您可以使用实用程序对象#lists来检查UI中的列表是否为空(为您节省一个布尔模型属性)

<tbody th:if="${not #lists.isEmpty(identificationData)}">
    <tr th:each="row : ${identificationData}">
        ...
    </tr>
</tbody>
<tbody th:if="${#lists.isEmpty(identificationData)}">
    <tr>
        <td colspan="3">No Data found</td>
    </tr>
</tbody>
相关问题