0

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?

1
  • will you accept my answer then? Commented Nov 19, 2019 at 11:02

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.