Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf Table issues with rowspan (1 Order, N Articles)

I'm making a simple intranet web view for a ordering system to show all orders that are currently processed. However I'm stuck with the thymeleaf markup:

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;
}

What i want to accomplish is the following (with 1 order + 3 articles in that order):

<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>

all the common stuff should be row-spanned over all articles and the articles should be viewed one each line. However i have no idea on how to accomplish that with two th:each (one for the order, one for the articles of the order). I could "flatten" out my view (each line represented by one Line-Object) with a ton of ifs in the markup but that is a very dirty hack-around in my opinion...

Can anyone help me out with a better solution?

Thanks a lot!

like image 317
semteX Avatar asked Jan 07 '23 11:01

semteX


1 Answers

 <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" for remove div after table generated.

Changed to avoid rendering problems. Thanks to comment of @Martin C.

like image 111
Ken Bekov Avatar answered Jan 15 '23 11:01

Ken Bekov