Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 773
James Avatar asked Jul 11 '17 21:07

James


1 Answers

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>
like image 176
James Avatar answered Nov 13 '22 14:11

James