How to display blank data in Thymeleaf in th:text - thymeleaf

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>

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>

The equals check must be inside the curly brackets like this:
<th>Approved Date</th>
<td th:if="${obj.statusNm=='Approved'}" th:text="${obj.modifyDt}"></td>
<td th:unless="${obj.statusNm=='Approved'}" th:text=""></td>

Related

Parallel loops in Thymeleaf

This is my code, it does not work. I know I can not put the loops that, but how should they be to get the logic done
<tr th:each="max:${top3max}", th:each="min:${top3min}">
<td th:text="${max.getName()}"></td>
<td th:text="${min.getName()}"></td>
</tr>
As long as both Lists are the same size, you can loop through one and use the status variable to access the other. Like this:
<tr th:each="max, i: ${top3max}">
<td th:text="${max.getName()}"></td>
<td th:text="${top3min[i.index]}"></td>
</tr>
If you want something more like a traditional for loop, this will work (as long as top3max is a List -- you'll have to use .length instead of .size() if you're dealing with an array.
<tr th:each="i: ${#numbers.sequence(0, top3max.size() - 1)}">
<td th:text="${top3max[i]}"></td>
<td th:text="${top3min[i]}"></td>
</tr>

Is commenting faster than th:remove="all-but-first"?

There's an example in the Thymeleaf docs that I'm curious about.
Is commenting out a block using Thymeleaf-style commenting faster than using th:remove="all-but-first"?
Example:
<table>
<tr th:each="user : ${users}">
<td th:text="${user.name}">Jamie Dimon</td>
</tr>
<!--/* Hidden from evaluation -->
<tr>
<td>Jeff Bezos</td>
</tr>
<tr>
<td>Warren Buffett</td>
</tr>
<!--*/-->
</table>
vs.
<table th:remove="all-but-first">
<tr th:each="user : ${users}">
<td th:text="${user.name}">Jamie Dimon</td>
</tr>
<tr>
<td>Jeff Bezos</td>
</tr>
<tr>
<td>Warren Buffett</td>
</tr>
</table>
In both cases, prototyping would show the same HTML, but I am wondering whether the low precedence of th:remove would make it less desirable since it would be removing the tags after evaluating the th:each.

How to apply if condition in table td in thymeleaf

I am new to thymeleaf, I have a little problem. I am successfully getting data from database and displaying to table formate, here i am getting true/false from database. Same thing displaying in table formate.
But i want to show true as yes and fasle as no in front end.
<tbody>
<tr th:each="trn,iterStat : ${trans}">
<td th:text="${trn.txn}">Yes</td>
</tr>
</tbody>
How to change my code?
Different ways to go about this depending on what you want your html to look like:
<tbody>
<tr th:each="trn,iterStat : ${trans}">
<td th:text="${trn.txn ? 'Yes' : 'No'}" />
</tr>
</tbody>
or
<tbody>
<tr th:each="trn,iterStat : ${trans}">
<td>
<span th:if="${trn.txn}">Yes</span>
<span th:unless="${trn.txn}">No</span>
</td>
</tr>
</tbody>

How to iterate through a list to find a match in thymeleaf?

How can I rewrite the script below with out specifying the index[x]? and allow thymeleaf to iterate the whole list to see if it find a match of test.host == server.host.
<tr th:if="${server.host == test[1].host}">
<td th:text ="${test[1].Status}"></td>
<td th:text="${test[1].host}"></td>
<td th:text="${test[1].version}"></td>
</tr>
It's not working the way I wanted it to work. It worked when I used index[x] to specify a host. I wanted it to iterate through the whole list. If test.host matches server.host. I don't want to specify an index[x]
I think you can iterate using th:each on your collection/ array
<th:block th:each="item: ${test}">///item is a var name, test is your array
<tr th:if="${server.host == item.host}">
<td th:text="${item.Status}"></td>
<td th:text="${item.host}"></td>
<td th:text="${item.version}"></td>
</tr>
</th:block>

Getting data in <tr> with thymeleaf

This is my code fragment:
<th:block th:each="d : ${items}">
<!--<tr>-->
<td th:text="${d.productname}"></td>
<td th:text="${d.quantity}"></td>
<td th:text="${d.price}"></td>
<!--</tr>-->
</th:block>
items is an ArrayList.
I can get data if the tr doesn't exist (as you can see I commented it in the code).
If I put them back, my th:text gets null values.
I also tried tr th:each but it didn't help.
How can I solve this problem? Thank you.
I am using thymeleaf-3.0.0 ,It worked for me.
<table>
<tr th:each="d : ${items}" >
<td th:text="${d.productname}"></td>
<td th:text="${d.quantity}"></td>
<td th:text="${d.price}"></td>
</tr>
</table>

Resources