行数表中的Thymeleaf表问题(1个订单,N个文章)

时间:2016-03-14 22:59:45

标签: java spring spring-mvc thymeleaf

我正在为订购系统制作一个简单的Intranet Web视图,以显示当前处理的所有订单。但是我坚持使用百万美元标记:

public class Order {
  private Factory factory;

  private String orderNumber;

  private Date orderDate;

  ....

  private List<Article> articles;
}

public class Article {

  private String number;

  private String name;
}

我想要完成的是以下内容(按顺序排列1个订单+ 3篇文章):

<table class="table table-striped table-hover table-middle table-condensed table-bordered">
  <thead>
    <tr>
      <th>OrderNr</th>
      <th>Date</th>
      <th>Article Number</th>
      <th>ArticleName</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td rowspan="3">Order 32</td>
      <td rowspan="3">27.03.2020</td>
      <td>17442</td>
      <td>Screws</td>
    </tr>
    <tr>
      <td>023423</td>
      <td>Potatoe</td>
    </tr>
    <tr>
      <td>32342</td>
      <td>YetAnotherItem</td>
    </tr>
  </tbody>
</table>

所有常见的东西都应该在所有文章上划线,文章应该每行查看一次。但是我不知道如何用两个来完成它:每个(一个用于订单,一个用于订单的文章)。我可以用标记中的大量ifs“展平”我的视图(由一个Line-Object表示的每一行)但在我看来这是一个非常脏的黑客...

任何人都可以帮助我找到更好的解决方案吗?

非常感谢!

1 个答案:

答案 0 :(得分:2)

 <table>
     <thead>
         <tr>
             <th>OrderNr</th>
             <th>Date</th>
             <th>Article Number</th>
             <th>ArticleName</th>
         </tr>
     </thead>
     <tbody>
        <div th:remove="tag" th:each="order:${orderList}" 
             th:with="articleCount=${order.articleList.size()}">
            <tr>
                <td th:text="${order.orderNumber}" th:rowspan="${order.articleList.size()}"></td>
                <td th:text="${order.orderDate}" th:rowspan="${order.articleList.size()}"></td>
                <td th:text="${articleCount>0}?${order.articles[0].number}:''"></td>
                <td th:text="${articleCount>0}?${order.articles[0].name}:''"></td>
            </tr>
            <tr th:each="article,stats:${order.articles}" th:if="${!stats.first}">
                <td th:text="${article.number}"></td>
                <td th:text="${article.name}"></td>
            </tr>
        </div>
     </tbody>
</table>

th:remove="tag"表格生成后删除div

更改以避免渲染问题。感谢@Martin C的comment