【问题标题】:How to display a collection in a single cell instead of creating a column for every element?如何在单个单元格中显示集合而不是为每个元素创建列?
【发布时间】:2019-11-10 16:01:26
【问题描述】:

我尝试在单个单元格中显示一个集合,但它为每个元素创建一个列。

我尝试了许多将 HTML 与 Thymeleaf 属性相结合的变体,但我不知道如何为 th:each 循环显示带有文本的单个单元格

<tr th:each="employee : ${allEmployees}">
    <td th:text="${employee.id}"></td>
    <td th:text="${employee.firstName}"></td>
    <td th:text="${employee.lastName}"></td>
    <td th:text="${employee.position}"></td>
    <td th:text="${employee.salary}"></td>
    <td th:text="${employee.supervisorId}"></td>
<!--Here-->
    <td th:each="team : ${employee.teams}" th:text="${team.name}"></td>
</tr>

我期待像 || 这样的东西技术架构,移动 || 但我不断得到 ||技术架构 ||移动 || 等(不介意昏迷和空格)

【问题讨论】:

    标签: html thymeleaf


    【解决方案1】:

    您正在指示 Thymeleaf 为每个团队创建 &lt;td&gt; 标签。相反,您可以创建一个 &lt;td&gt; 标记并在其中迭代:

    <tr th:each="employee : ${allEmployees}">
        <td th:text="${employee.id}">[id]</td>
        <td th:text="${employee.firstName}">[first]</td>
        <td th:text="${employee.lastName}">[last]</td>
        <td th:text="${employee.position}">[position]</td>
        <td th:text="${employee.salary}">[salary]</td>
        <td th:text="${employee.supervisorId}">[supervisor id]</td>
    <!--Here-->
        <td>
            <th:block th:each="team : ${employee.teams}">
              <span th:text="${team.name}" tag="remove">[team name]</span>
            </th:block>
        </td>
    </tr>
    

    如果您的列表中需要逗号,您可以查看this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2016-02-15
      相关资源
      最近更新 更多