【问题标题】:Multiple items in table cell表格单元格中的多个项目
【发布时间】:2022-01-08 00:45:42
【问题描述】:

我正在使用 thymeleaf 开发一个网站,到目前为止它对我来说一直运行良好,但是当我想在表格中单独显示多个项目时,它会在存在值的行上添加额外的单独单元格。到目前为止,我还没有解决这些问题,如果其他人可以看到我所缺少的,我将不胜感激。

提前致谢!

代码(已编辑)

在这里,我首先检查是否有处理程序(在数据库中),然后我使用 foreach 将它们全部放在一个列表中,但我无法将所有项目放在单个单元格中(或此处由标签 )。我已经将逻辑放在标签中,它工作得很好,但是没有数据的行会得到额外的单元格,如下图所示。

<td > th:if="${place.getHandler} != null" th:each="handlerList : ${place.getHandler}" th:text="${handlerList.name}"></td>

新代码

<td> <span th:if="${place.getHandler} != null" th:each="handlerList : ${place.getHandler}" th:text="${handlerList.name}"></span></td>

整个代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" class="has-navbar-fixed-top">
<head th:replace="common_fragments/header :: header">
    <meta charset="utf-8">
    <link rel="stylesheet" href="../../../../public/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="../../../../public/css/bulma_custom.min.css"/>
</head>
<body>

<div id="navbar-top">
    <nav th:replace="logged_in/admin/fragments/navbar :: nav"></nav>
</div>
<main>
    <section class="section">
        <div class="container">
            <h1 class="title">
                 matchade studenter
            </h1>
            <hr>
            <div class="content is-medium">
                <table id="table" class="table is-bordered is-narrow is-hoverable">
                    <thead>
                    <tr>
                        <th>Student</th>
                        <th>Student email</th>
                        <th>Student mobilnummer</th>
                        <th>Enhet</th>
                        <th>Handledare</th>
                        <th>Handledare email</th>
                        <th>Handledare mobilnummer</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="place : ${places}">
                        <td th:text="${place.student.studentData.name}"></td>
                        <td th:text="${place.student.studentData.email}"></td>
                        <td th:if="${place.student.studentData.phoneNumber} != ''" th:text="${place.student.studentData.phoneNumber}"></td>

                        <td th:if="${place.student.studentData.phoneNumber} == ''" >
                            <p class="icon has-text-danger">
                                <i class="fa fa-lg fa-times"></i>
                            </p>

                        </td>



                        <td th:text="|${place.unit.name} (Regioner: ${place.unit.municipality.getRegionNamesString}, Kommuner:  ${place.unit.municipality.name})|"></td>

                        <td> <span th:if="${place.getHandledare} != null" th:each="handledareList : ${place.getHandledare}" th:text="${handledareList.name}"></span></td>
                        <td th:if="${place.getHandledare} != null" th:text="${place.getHandledare[0].email}"></td>


                        <td th:if="${place.getHandledare} == null">
                            <p class="icon has-text-danger">
                                <i class="fa fa-lg fa-times"></i>
                            </p>
                        </td>

                        <td th:if="${place.getHandledare} == null">
                            <p class="icon has-text-danger">
                                <i class="fa fa-lg fa-times"></i>
                            </p>
                        </td>


                        <div th:if="${place.getHandledare} != null">
                        <td th:if="${place.getHandledare[0].phoneNumber} != ''" th:text="${place.getHandledare[0].phoneNumber}"></td>
                        </div>

                        <div th:if="${place.getHandledare} == null">
                            <td >
                                <p class="icon has-text-danger">
                                    <i class="fa fa-lg fa-times"></i>
                                </p>

                            </td>
                        </div>

                        <div th:if="${place.getHandledare} != null">
                            <td th:if="${place.getHandledare[0].phoneNumber} == '' ">

                            <p class="icon has-text-danger">
                                    <i class="fa fa-lg fa-times"></i>
                                </p>
                            </td>

                            </td>
                        </div>



                    </tr>
                    </tbody>

                </table>
                <br>
                <button class="button is-large is-success"  id="download-button">ladda ner matchning resultat</button>
                <br>
                <br>

            </div>
        </div>
    </section>
</main>

<footer th:replace="common_fragments/footer :: footer"></footer>




<script>
    function htmlToCSV(html, filename) {
        var data = [];
        var rows = document.querySelectorAll("table tr");

        for (var i = 0; i < rows.length; i++) {
            var row = [], cols = rows[i].querySelectorAll("td, th");

            for (var j = 0; j < cols.length; j++) {
                row.push(cols[j].innerText);
            }

            data.push(row.join(";"));
        }

        downloadCSVFile(data.join("\n"), filename);
    }
</script>


<script>
    function downloadCSVFile(csv, filename) {
        var csv_file, download_link;

        csv_file = new Blob(["\uFEFF"+csv], {type: "text/csv"});

        download_link = document.createElement("a");

        download_link.download = filename;

        download_link.href = window.URL.createObjectURL(csv_file);

        download_link.style.display = "none";

        document.body.appendChild(download_link);

        download_link.click();
    }
</script>

<script>
    document.getElementById("download-button").addEventListener("click", function () {
        var html = document.querySelector("table").outerHTML;
        htmlToCSV(html, "matchning.csv");
    });
</script>

</body>
</html>

【问题讨论】:

    标签: java html database web thymeleaf


    【解决方案1】:

    您可以将 Thymeleaf 逻辑从 &lt;td&gt; 标签移动到 &lt;td&gt; 标签内的标签中 - 例如,&lt;span&gt;

    <td>
        <span th:if="${place.getHandler} != null" 
              th:each="handlerList : ${place.getHandler}" 
              th:text="${handlerList.name}"></span>
    </td>
    

    您可以从那里添加您可能需要格式化跨度的任何 CSS。

    如果您有额外的&lt;td&gt; 单元格需要抑制,则将th:if 表达式移动到&lt;td&gt; 标记内:

    <td th:if="${place.getHandler} != null">
        <span th:each="handlerList : ${place.getHandler}" 
              th:text="${handlerList.name}"></span>
    </td>
    

    【讨论】:

    • 谢谢!这行得通,但我现在在我不想在该列中包含数据的行上获得额外的单元格,基本上是可以在其单元格中保存额外数据的列,当单元格为空时,在行上添加额外的单元格。
    • 您需要通过编辑您的问题(使用代码和数据)而不是 - 或者以及 - 描述问题来表明您的意思。我认为我理解您所描述的内容,但是如果没有明确的示例,很难确定。也许您只需要将 th:if="${place.getHandler} != null"&lt;span&gt; 移至 &lt;td&gt; - 但请在问题中显示一个示例,以便清楚。
    • 我的意思是,按照您使用 span 的示例进行操作,但我在行上获得了额外的单元格,而单元格中没有包含多个项目的数据。上面添加的图片。
    • 添加了整个代码。
    • 如果您在实际表格中看到if 语句,那么听起来您没有将该代码放在&lt;td&gt; 开始标记内。我更新了我的答案。
    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多