【问题标题】:How to do a if else in thymeleaf inside a loop that populate a table如何在填充表格的循环内的百里香中执行 if else
【发布时间】:2014-11-21 14:55:20
【问题描述】:

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

EDITED:代码已被编辑并可以正常工作

【问题讨论】:

    标签: html spring-mvc html-table thymeleaf


    【解决方案1】:

    只需将您的代码更改为:

    <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 部分文档了解更多详情。

    【讨论】:

    • 谢谢我用你的建议编辑了我的代码,它工作了
    猜你喜欢
    • 2019-01-04
    • 1970-01-01
    • 2015-06-16
    • 2014-01-05
    • 1970-01-01
    • 2014-11-14
    • 2022-10-08
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多