html - 如何在填充表的循环内的 thymeleaf 中执行 if else

标签 html spring-mvc html-table thymeleaf

我正在使用 <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/

相关文章:

没有注释的 Spring MVC - 未找到 HTTP 404 页面错误即将到来

python - 平面 HTML 页面的搜索索引

java - 如何在 spring 中创建一个非阻塞的@RestController webservice?

ios - 从 phonegap1.2 加载远程 URL

javascript - jsp页面只显示视频缩略图

css - 如何正确使用 <col> 标签,所有浏览器都支持吗?

css - 单击 Angular 2 时如何调整表格或 div 的大小?

html - 当我将鼠标悬停在 html 表中时更改单元格的颜色

javascript - Jquery - 在字符串中强制换行时遇到问题

html - CSS 图像 :after not working on Coppermine