2014-09-26 40 views
0

我正在使用<tr th:each>填充表,如果值爲null,我希望將if語句計算爲空值想要把這個「 - 」而不是「null」。如何在填充表的循環內的thymeleaf中執行if

我如何使用th:if或其他類似的功能來做到這一點,我是新的使用百里香?

這是我的代碼:

<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> 

編輯:代碼被編輯及其工作

回答

2

只需更改您的代碼:

<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> 

甚至更​​多consisely你可以寫:

<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> 

這工作,因爲${nodeInfo.name}進行評估,以truename不爲空

你也可以探索,而不是使用!=

退房有關更多詳細信息的文件this部分使用th:unless

+0

感謝我編輯我的代碼與您的建議,它的工作 – stackUser2000 2014-09-26 15:16:28

+0

很高興聽到它! – geoand 2014-09-26 16:07:19