问题
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