Thymeleaf如何控制表格對于TR樣式

首先,吐槽一下。這個問題搞得我很無語。最后有種撞了南墻的感覺。

問題描述

眾所周知,Thymeleaf框架的th:each標簽很好用,相較于freemaker而言,更加簡練。但是如果遇到,表格對應tr隔行變色的問題,就顯得有些力不從心了。
比如:

 <div th:if="${typeList!=null && typeList.size()>0}">
  <div th:each="type,index:${typeList}" >
  <tr th:if="${index.odd}" class="odd">
                           <td align="center" th:text="${type.typeId}"></td>
                           <td align="center" th:text="${type.typeName}"></td>
                           <td align="center">
                               <button th:onclick="'javascript:window.location.href=\'/admin/website/imagesPage?websiteImages.typeId='+${type.typeId}+'\''" class="ui-state-default ui-corner-all" type="button">查看廣告圖</button>
                               <button th:onclick="'javascript:updateType('+${type.typeId}+',this)'" class="ui-state-default ui-corner-all" type="button">修改名稱</button>
                               <button th:onclick="'javascript:deleteType('+${type.typeId}+')'" class="ui-state-default ui-corner-all" type="button">刪除</button>
                           </td>
                       </tr>
</div>
 <tr th:unless="${index.odd}" class="odd">
                           <td align="center" th:text="${type.typeId}"></td>
                           <td align="center" th:text="${type.typeName}"></td>
                           <td align="center">
                               <button th:onclick="'javascript:window.location.href=\'/admin/website/imagesPage?websiteImages.typeId='+${type.typeId}+'\''" class="ui-state-default ui-corner-all" type="button">查看廣告圖</button>
                               <button th:onclick="'javascript:updateType('+${type.typeId}+',this)'" class="ui-state-default ui-corner-all" type="button">修改名稱</button>
                               <button th:onclick="'javascript:deleteType('+${type.typeId}+')'" class="ui-state-default ui-corner-all" type="button">刪除</button>
                           </td>
                       </tr>
</div>
 </div>

如上代碼可以實現隔行變色嗎?
答案好像不是很樂觀。報錯了。提示index為null。

原因分析

分析代碼,感覺沒有問題。
可是,經過嘗試,發(fā)現:如果th:each標簽搭配tr和td出現的話,必須作用在tr之前,不可加在div上。

解決方案

將th:each標簽加在tr上之后,可以直接取值index,即行狀態(tài)。然后搭配th:class標簽即可實現上述功能。
該問題折騰了整整一晚上,完全沒想到會有這種操作。

  <div th:if="${typeList!=null && typeList.size()>0}">
                       <tr th:class="${index.odd}?'odd'" th:each="type,index:${typeList}">
                           <td align="center" th:text="${type.typeId}"></td>
                           <td align="center" th:text="${type.typeName}"></td>
                           <td align="center">
                               <button th:onclick="'javascript:window.location.href=\'/admin/website/imagesPage?websiteImages.typeId='+${type.typeId}+'\''" class="ui-state-default ui-corner-all" type="button">查看廣告圖</button>
                               <button th:onclick="'javascript:updateType('+${type.typeId}+',this)'" class="ui-state-default ui-corner-all" type="button">修改名稱</button>
                               <button th:onclick="'javascript:deleteType('+${type.typeId}+')'" class="ui-state-default ui-corner-all" type="button">刪除</button>
                           </td>
                       </tr>
        </div>
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 一. 什么是Thymeleaf Thymeleaf是面向Web和獨立環(huán)境的現代服務器端Java模板引擎。Thyme...
    低調的微胖閱讀 66,309評論 8 69
  • Thymeleaf簡介 前面的例子我們使用的視圖技術主要是JSP。JSP的優(yōu)點是它是Java EE容器的一部分,幾...
    樂百川閱讀 9,156評論 2 56
  • Thymeleaf 一直以來都是個使用小眾的模板引擎,在2.0以前,最為人吐槽的是性能跌到無底線。甚至朋友的項目因...
    閑大賦閱讀 7,420評論 1 12
  • Thymeleaf可以用 th 標簽動態(tài)替換掉靜態(tài)數據了。如下圖,后臺傳出的message會將靜態(tài)數據“Red C...
    Mango_yes閱讀 814評論 1 0
  • Thymeleaf 簡易教程 本文源碼可以在這里下載: https://github.com/codergege/...
    codergege閱讀 50,837評論 4 30

友情鏈接更多精彩內容