【问题标题】:Add a condition to th:text on thymeleafth:text on thymeleaf 添加条件
【发布时间】:2017-10-05 17:03:30
【问题描述】:

我的 html 文件中有一个表格,其填充如下:

<table>
        <thead>
        <tr>
            <th class="text-left">First Name</th>
            <th class="text-left">Last Name</th>
            <th class="text-left">Status</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="user : ${users}">
            <td class="text-left" th:text="${user.firstname}"></td>
            <td class="text-left" th:text="${user.lastname}"></td>
            <td class="text-left" th:text="${user.status}"></td>
        </tr>
        </tbody>
    </table>

问题在于,对于状态,我有两个条件:在线和离线,我想做的是如果 status=='online' 将 th:text 颜色更改为绿色,并且当 status=='offline' 将其更改为红色的。我不知道是否有办法用百里香来做到这一点,或者我必须使用 javascript。

【问题讨论】:

    标签: javascript html spring thymeleaf


    【解决方案1】:

    您是否尝试过使用th:classappend?您可以使用您的规则定义两个 CSS 类(颜色:红色;或颜色:绿色;)并根据条件分配正确的类:

    <td class="text-left" th:text="${user.firstname}" th:classappend="${isOnline} ? onlineCSSClass : offlineCSSClass"></td>
    

    附言我认为isOnline 是一个设置为真/假的变量,具体取决于它是在线还是离线

    【讨论】:

    • 实际上状态是一个字符串而不是布尔值
    • 只需在您的 java 代码中定义一个 bool 变量,如:boolean isOnline = status == "online"; 并在条件类附加中使用该布尔变量
    猜你喜欢
    • 2015-10-31
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多