简体   繁体   中英

Conditional inline formatting with thymeleaf attributes

I want to conditionally style elements by comparing two attributes.

  1. a url parameter 'customerID'

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

  1. a model attribute

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

I want to change the background of a button if these two parameters are equal. I'm using inline styling with 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.

    <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?

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' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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