根据条件百里香叶更改 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>

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

对于任何好奇的人:

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

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

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

这是

预期的行为(即更改单元格的背景,而不是整行),因为您在单个td中使用跨度。也许您应该考虑将条件样式移动到tr(行标记)作为以下代码示例:

<tr th:style="${taskstatistics.getActual()} == ${taskstatistics.getExpected()} ? background-color: green' : 'background-color: red'">

最新更新