如何在填充表的循环内的百里香中做if if [英] How to do a if else in thymeleaf inside a loop that populate a table
问题描述
我正在使用< tr th:each>
填充表,并且我想放置一个if语句来评估该值(如果该值为null),如果该值为null我想将此-"代替"null".
I'm populating a table using a <tr th:each>
and I want to put a if statement that evaluates the value if it is null, if the values is null I want to put this "--" instead of "null".
我该如何使用 th:if
或其他类似的功能(使用百里香新手)来实现此目的?
How can I do this using the th:if
or other similar function I am new using thymeleaf?
这是我的代码:
<table id="datatable_fixed_column" class="table table-striped table-bordered" width="100%">
<thead>
<tr>
<th>name</th>
<th>lastname</th>
<tr>
<tbody>
<tr th:each="nodeInfo : ${listOfData}">
<td th:if="${nodeInfo.name} == 'null'"> -- </td>
<td th:if="${nodeInfo.name} != 'null'" th:text="${nodeInfo.name}"></td>
已代码已编辑且可以正常工作
EDITED: the code was edited and its working
推荐答案
只需将代码更改为:
<tr th:each="nodeInfo : ${listOfData}">
<td th:if="${nodeInfo.name} == null">This is the value if the name is null</td>
<td th:if="${nodeInfo.name} != null">This is the value if the name is NOT null</td>
</tr>
或更简而言之,您可以写:
Or even more consisely you could write:
<tr th:each="nodeInfo : ${listOfData}">
<td th:if="!${nodeInfo.name}">This is the value if the name is null</td>
<td th:if="${nodeInfo.name}">This is the value if the name is NOT null</td>
</tr>
之所以有效,是因为当 name
不为空时,将 $ {nodeInfo.name}
评估为 true
which works because ${nodeInfo.name}
is evaluated to true
when name
is not null
您也可以探索使用 th:除非
,而不是使用!=
You could also explore the use of th:unless
instead of using !=
查看这部分文档以获取更多详细信息.
Check out this part of the documentation for more details.
这篇关于如何在填充表的循环内的百里香中做if if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!