Search code examples
thymeleaf

How to display blank data in Thymeleaf in th:text


I am trying to use Thymeleaf if and unless but it is not working. What I am trying to do is I want to check if the statusNm variable has a value of 'Approved' then it will display the content otherwise it will leave it blank.

But I am not able to achieve it. Please suggest me a solution.

<th>Approved Date</th>
<td th:if="${obj.statusNm}=='Approved'" th:text="${obj.modifyDt}"></td>
<td th:unless="${obj.statusNm}" th:text=""></td>
<th>Approved By</th>
<td th:if="${obj.statusNm}=='Approved'" th:text="${obj.modifier}"></td>
<td th:unless="${obj.statusNm}" th:text=""></td>

Solution

  • As the alternative, you may look at Conditional expressions and evaluate your condition directly in th:text attribute ...

    <th>Approved Date</th>
    <td th:text="${obj.statusNm=='Approved'} ? ${obj.modifyDt}"></td>
    <th>Approved By</th>
    <td th:text="${obj.statusNm=='Approved'} ? ${obj.modifier}"></td>