简体   繁体   English

具有 thymeleaf 属性的条件内联格式

[英]Conditional inline formatting with thymeleaf attributes

I want to conditionally style elements by comparing two attributes.我想通过比较两个属性来有条件地设置元素的样式。

  1. a url parameter 'customerID'一个 url 参数“客户 ID”

http://localhost:8080/home?customerID=3 http://localhost:8080/home?customerID=3

  1. a model attribute一个 model 属性

th:each=" customer: ${customers}" th:each="客户:${客户}"

I want to change the background of a button if these two parameters are equal.如果这两个参数相等,我想更改按钮的背景。 I'm using inline styling with thymeleaf.我正在使用 thymeleaf 的内联样式。

th:style="${param.customerID == customer.id ? 'background-color:green' : 'background-color:red'}"

but the result of the condition is always false even when the two arguments are equal.但即使两个 arguments 相等,条件的结果也始终为假。

    <div class="user-list">
    <div class="active-btn-group" id="active-button-group" th:each=" customer : ${customers}">

        <p th:text="${param.customerID}">Test</p>
        <p th:text="${customer.id}">Test</p>
        <button th:id="${customer.id}" th:style="${param.customerID == customer.id ? 'background-color:green' : 'background-color:red'}">

        </button>
    </div>
</div>

How should I change the about inline formatting expression?我应该如何更改 about 内联格式表达式?

I think there is a type conversion problem.我认为存在类型转换问题。 would you try like bellow th:style="${#strings.equals(#strings.toString(param.customerID), #strings.toString(customer.id))?'background-color:green': 'background-color:red' }你会尝试像下面这样th:style="${#strings.equals(#strings.toString(param.customerID), #strings.toString(customer.id))?'background-color:green': 'background-color:red' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM