Thymeleaf - How to apply two (or more) styles based on mutually exclusive conditions

↘锁芯ラ 提交于 2021-01-19 04:04:23

问题


I need to have rows in a table alternate background color. I also need to have the text color in the rows be dependent on a value. How can I do this using Thymeleaf? Here's the code that I have:

<tr th:each="item, rowStat : ${items}" 
    th:style="${rowStat.odd} ? 'background: #f0f0f2;' : 'background: #ffffff;'"
    th:style="${item.getValue()} > 5 ? 'color: red;' : 'color: black;'">

                <td.... <!-- cols>

</tr>

This doesn't work though. Thymeleaf give a parsing error : Attribute "th:style" was already specified for element "tr".

Update

Meant to note that this an HTML email so I need to use inline styles.


回答1:


Thymeleaf has a th:styleappend attribute that allows for multiple styles to be applied:

<tr th:each="item, rowStat : ${items}" 
    th:style="${rowStat.odd} ? 'background: #f0f0f2;' : 'background: #ffffff;'"
    th:styleappend="${item.getValue()} > 5 ? 'color: red;' : 'color: black;'">

                <td.... <!-- cols>

</tr>


来源:https://stackoverflow.com/questions/45044821/thymeleaf-how-to-apply-two-or-more-styles-based-on-mutually-exclusive-condit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!