我正在eclipse中创建一个项目,希望在表格中显示输入到表单中的值。我希望表只显示有值的行,但不知道如何做到这一点。到目前为止,我刚刚添加了多个行,并且为了不显示它们而使用了display:none
,但是我想不出一种让th:text="${name1}"
显示的方法。
下面是我到目前为止使用的方法的一个例子:
<table id="table">
<tr id="tableRow">
<th class="tableHeader">Name</th>
<th class="tableHeader">Description</th>
</tr>
<tr id="tableRow" style="display:none">
<td class="tableCell" th:text="${name1}"></td>
<td class="tableCell" th:text="${description1}"></td>
</tr>
<tr id="tableRow" style="display:none">
<td class="tableCell" th:text="${name2}"></td>
<td class="tableCell" th:text="${description2}"></td>
</tr>
<tr id="tableRow" style="display:none">
<td class="tableCell" th:text="${name3}"></td>
<td class="tableCell" th:text="${description3}"></td>
</tr>
</table>
我对编码比较陌生,因此任何帮助都将不胜感激。
发布于 2017-12-07 00:21:42
我找到了一个使用th:each
的好方法。
<table id="table">
<tr id="tableRow">
<th class="tableHeader">Name</th>
<th class="tableHeader">Description</th>
</tr>
<tr id="tableRow" th:each="inputMap : ${InputMap}">
<td class="tableCell" th:text="${inputMap.value.name}"></td>
<td class="tableCell" th:text="${inputMap.value.description}"></td>
</tr>
</table>
${InputMap}
来自我的控制器:
@GetMapping("/tablePage")
public void getTableData(Model model) {
inputRepository.findAll().forEach(inputObject -> {
inputMap.put(inputObject.id, inputObject);
});
if (inputMap != null){
inputMap.forEach((id, i) -> {
model.addAttribute(id + "name", id + i.getName());
model.addAttribute(id + "description", id + i.getDescription());
});
}
model.addAttribute("InputMap", inputMap);
}
发布于 2017-12-06 20:31:13
您需要在使用它之前测试值
<table id="table">
<tr id="tableRow">
<th class="tableHeader">Name</th>
<th class="tableHeader">Description</th>
</tr>
<tr id="tableRow" th:if="${name1 != null or description1 != null}">
<td class="tableCell" th:text="${name1}"></td>
<td class="tableCell" th:text="${description1}"></td>
</tr>
<tr id="tableRow" th:if="${name2 != null or description2 != null}">
<td class="tableCell" th:text="${name2}"></td>
<td class="tableCell" th:text="${description2}"></td>
</tr>
<tr id="tableRow" th:if="${name3 != null or description3 != null}">
<td class="tableCell" th:text="${name3}"></td>
<td class="tableCell" th:text="${description3}"></td>
</tr>
</table>
https://stackoverflow.com/questions/47672964
复制相似问题