美文网首页程序员
Thymeleaf如何控制表格对于TR样式

Thymeleaf如何控制表格对于TR样式

作者: OnyWang | 来源:发表于2018-01-31 08:39 被阅读1601次

首先,吐槽一下。这个问题搞得我很无语。最后有种撞了南墙的感觉。

问题描述

众所周知,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。

原因分析

分析代码,感觉没有问题。
可是,经过尝试,发现:如果th:each标签搭配tr和td出现的话,必须作用在tr之前,不可加在div上。

解决方案

将th:each标签加在tr上之后,可以直接取值index,即行状态。然后搭配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>

相关文章

  • Thymeleaf如何控制表格对于TR样式

    首先,吐槽一下。这个问题搞得我很无语。最后有种撞了南墙的感觉。 问题描述 众所周知,Thymeleaf框架的th:...

  • Bootstrap表格和按钮(三)

    1.表格 tablethead tr thtbody tr td 基本样式 ,都要基于此样式 1.1 条纹状表格 ...

  • html基本标签

    html 表格 表格标签 th 表头 td表格数据 tr表格的行和列数 定义表格的样式 在style里面 tab...

  • HTML表格

    2019-04-11 table 表格 tr行 td列(单元格) 外观样式属性 border属性 ​ 表格框线...

  • PPT-Lecture07-表格

    1、如何插入表格? “插入”->"表格"->给定行列即可。 2、插入表格之后,如何修改表格的样式? 选中表格,->...

  • Html/CSS----表格与表单

    表格 table、tr、th、td 使用table标签创建一个表格。 tr表示表格中的一行。 tr中可以编写一个或...

  • iview table响应式问题

    问题 表格样式重叠如图所示: 研究过程 控制台调试发现,table宽度不能自适应如图: 解决办法: 更改表格样式

  • thymeleaf 和layui 冲突

    [[]] 冲突 thymeleaf 和layui表格都对[[]] 有定义,thymeleaf定义比较严格,lay...

  • table表格的使用

    01表格-table常见元素 table tr td 02表格-tr和td相关属性设置 03表格-边框合并属性 b...

  • thymeleaf动态添加class

    thymeleaf 怎样动态添加class样式

网友评论

    本文标题:Thymeleaf如何控制表格对于TR样式

    本文链接:https://www.haomeiwen.com/subject/kjxkzxtx.html