我正在使用 <tr th:each>
填充表格我想放置一个 if 语句来评估该值是否为 null,如果该值为 null,我想放置此“--”而不是“null”。
如何使用 th:if
执行此操作或其他类似的功能我是新使用 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>
已编辑:代码已编辑及其工作
最佳答案
只需将代码更改为:
<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>
或者更简洁地你可以写:
<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
您还可以探索使用 th:unless
代替 !=
查看this部分文档以获取更多详细信息。
关于html - 如何在填充表的循环内的 thymeleaf 中执行 if else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26062218/