【问题标题】:Thymeleaf: th:each for two table rows?Thymeleaf: th:each 两个表行?
【发布时间】:2020-10-09 17:04:46
【问题描述】:

如何在th:each 循环中创建一组两行而不是一行?

我知道我能做到:

<tr th:each="obj: ${listOfObjects}">
   <td>a column with data: ${obj.id}</td>
</tr>

但是,我想要创建两个 &lt;tr&gt; 元素,就像我对 JSTL 所做的那样:

<c:forEach items="${listOfObjects}" var="obj">
    <tr>
       <td>${obj.id}</td>
    </tr>
    <tr>
       <td>${obj.name}</td>
    </tr>
</c:forEach>

有没有办法通过 Thymeleaf 实现这一目标?

【问题讨论】:

    标签: java spring thymeleaf


    【解决方案1】:

    您可以使用 th:block 元素将行组合在一起并重复它们:

    <th:block th:each="obj: ${listOfObjects}">
        <tr>
           <td th:text="${obj.id}"></td>
        </tr>
        <tr>
           <td th:text="${obj.name}"></td>
        </tr>
    </th:block>
    

    你可以阅读更多关于th:blockhere的信息

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 2018-05-09
      • 2021-06-10
      • 2018-05-11
      • 1970-01-01
      • 2023-03-31
      • 2015-06-17
      • 2016-07-16
      • 1970-01-01
      相关资源
      最近更新 更多