首页 > 解决方案 > 根据条件百里香更改 tr 背景颜色

问题描述

我希望根据 th:if 条件更改表格行的背景颜色。

例如:如果预期 != 实际,则 bg = 红色。反之亦然,bg = 绿色。

对于我的示例,我目前有三列——评估类型、预期和实际。评估类型确定预期值和实际值。当前跨度th:style正确突出显示各个单元格,但我希望<tr>根据评估使整个突出显示的红色或绿色。

当前代码:

<thead> 
    <th>Eval Type </th>
    <th>Expected </th>
    <th>Actual </th>
</thead>
<tbody>
<tr>
    <td>
                    <span th:if="${example.getEvaluationType()} == '1'" th:text="${example.getExpectedcount()}" 
                    th:style="${example.getActualcount()} == ${example.getExpectedcount()} ? 'background: green' : 'background: red'"></span>

                    <span th:if="${example.getEvaluationType()} == '2'" th:text="${example.getExpectedmb()}"
                    th:style="${example.getActualmb()} == ${example.getExpectedmb()} ? 'background: green' : 'background: red'"></span>

                    <span th:if="${example.getEvaluationType()} == '3'" th:text="${example.getExpectedminutes()}" 
                    th:style="${example.getActualminutes()} == ${example.getExpectedminutes()} ? 'background: green' : 'background: red'" ></span>
    </td>
</tr>
</tbody>

感谢任何有关问题的帮助或线索,因为我没有找到任何问题。谢谢!

标签: springspring-mvcthymeleaf

解决方案


对于任何好奇的人:

我最终完成了上面的计算并在我的后端完成了所有这些actual == expected ? 'TRUE' : 'FALSE'。我将输出分配给单个变量passed

将变量和正确的 get/setter 添加到您的程序中,然后此行将使输出能够将整行着色为红色/绿色。

<tr th:style="${taskstatistics.getPassed()} == FALSE ? 'background: red' : 'background: green'">


推荐阅读