【问题标题】:Perform conditions in ThymeLeaf in Spring Boot project在 Spring Boot 项目中的 ThymLeaf 中执行条件
【发布时间】:2021-05-07 09:08:42
【问题描述】:

我正在使用 Spring Boot Thymeleaf 并希望做出基于条件的决定。如果条件匹配,则整行应为绿色,否则为红色

<body>
    <div class="container my-2">
     <h1>Active Workflow Details</h1>
    <div class="card">
    <div class="card-body">
        <div th:switch="${mdsAcives}" class="container my-5">
            <div class="col-md-10">
                <h2 th:case="null">No record found !!</h2>
                <div th:case="*">
                    <table border="1" class="table table-striped table-responsive-md">
                        <thead>
                            <tr>
                                <th>Mapping Name</th>
                                <th>Type of Mappings</th>
                                <th>Table Names</th>
                                <th>System 1 Count </th>
                                <th>System 2 Count </th>
                                <th>Actions</th>
                            </tr>
                         </thead>
                        <tbody>
                            <tr th:each="m : ${mdsAcives}">
                                <td th:text="${m.mappingName}"></td>
                                <td th:text="${m.type}"></td>
                                <td th:text="${m.tableName}"></td>
                                <td th:text="${m.system1Cnt}"></td>
                                <td th:text="${m.system2Cnt}"></td>
                                <td th:style="${m.system1Cnt} eq ${m.system2Cnt} ? 'color: red;' : 'color: green;'}}" th:text="TRUE or FALSE"></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    </div>
    </div>
</body>

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    你可以这样做:

    <tr th:class="${your condition}? 'green' : 'red'">
    

    我假设您已经定义了两个 CSS 类来使行变为绿色或红色。

    【讨论】:

    • 即使这样也行不通&lt;td th:style="${m.system1Cnt == m.system2Cnt ? 'color: red;' : 'color: green;'}}" th:text="${m.clCnt}"&gt;&lt;/td&gt;
    • 请仔细阅读我的例子。 1)它的 th:class 呈现类属性。 2) 条件进入 ${}
    • 是的,这有什么问题? &lt;td th:class="${m.system1Cnt== m.system2Cnt} ? 'red' : 'green'}" th:text="${m.clCnt}"&gt;&lt;/td&gt;
    • 如何比较两个长值?
    • m.system1Cnt.equals(m.system2Cnt)
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多