首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >值为null时如何隐藏表行

值为null时如何隐藏表行
EN

Stack Overflow用户
提问于 2017-12-06 19:12:46
回答 2查看 1.1K关注 0票数 1

我正在eclipse中创建一个项目,希望在表格中显示输入到表单中的值。我希望表只显示有值的行,但不知道如何做到这一点。到目前为止,我刚刚添加了多个行,并且为了不显示它们而使用了display:none,但是我想不出一种让th:text="${name1}"显示的方法。

下面是我到目前为止使用的方法的一个例子:

代码语言:javascript
复制
<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>

我对编码比较陌生,因此任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-07 00:21:42

我找到了一个使用th:each的好方法。

代码语言:javascript
复制
    <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}来自我的控制器:

代码语言:javascript
复制
@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);
}
票数 0
EN

Stack Overflow用户

发布于 2017-12-06 20:31:13

您需要在使用它之前测试值

代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47672964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档