I use a normal html table for iterating through a list of objects in thymeleaf.
I want to change the class of the <tr>
tag changing by the value of the providerResponse.status
. But this value is first known after the start of the iteration. So I think its not usable in the same <tr>
row definition.
I also used a local variable for switching the <td>
class. But the local variable is just usable in the context of the used html attribute. So I needed to write the code several times.
Is it possible to use the local variable in the full context of the table?
Is there a way to reduce the duplication of the same code?
<tr th:each="providerResponse : ${providerResponses}">
<th:block th:switch="${providerResponse.status}"
th:with="temp='active'">
<th:block th:case="'AVAILABLE'">
<th:block th:with="temp='success'">
<td th:class="${temp}" th:text="${providerResponse.executeDate}"></td>
<td th:class="${temp}" th:text="${providerResponse.provider}"></td>
<td th:class="${temp}" th:text="${providerResponse.status}"></td>
</th:block>
</th:block>
<th:block th:case="'UNKNOWN'">
<th:block th:with="temp='danger'">
<td th:class="${temp}" th:text="${providerResponse.executeDate}"></td>
<td th:class="${temp}" th:text="${providerResponse.provider}"></td>
<td th:class="${temp}" th:text="${providerResponse.status}"></td>
</th:block>
</th:block>
</th:block>
</tr>