Переберите список и создайте новую строку на основе шаблона Thymeleaf - PullRequest
0 голосов
/ 05 мая 2018

Я перебираю свой список продуктов и устанавливаю переменные, подобные этой

List<InvoiceEntry> products = invoice.getProducts();

  for (InvoiceEntry product : products) {
      BigDecimal netValue = product.getProduct().getNetValue()
          .setScale(2, RoundingMode.HALF_UP);
      double vatRate = product.getProduct().getVatRate().getVatPercent();
      BigDecimal vatValue = netValue.multiply(BigDecimal.valueOf(vatRate))
          .setScale(2, RoundingMode.HALF_UP);
      long amount = product.getAmount();

      context.setVariable("productName",product.getProduct().getName());
      context.setVariable("amount", product.getAmount());
      context.setVariable("retailPrice", netValue + "zł");
      context.setVariable("productNetValue",
          netValue.multiply(BigDecimal.valueOf(amount)) + "zł");
      context.setVariable("productVatRate", (int) (vatRate * 100) + "%");
      context.setVariable("productVatValue",
          vatValue.multiply(BigDecimal.valueOf(amount)) + "zł");
      context.setVariable("total",
          netValue.add(vatValue).multiply(BigDecimal.valueOf(amount)) + "zł");
    }

    String body = templateEngine.process("template", context);
    emailSender.sendEmail("someEmail@gmail.com", "some title", body);
    return "index";

фрагмент шаблона

<table width="100%" cellpadding="0" cellspacing="0">
  <tr bgcolor="D0D0D0">
    <td height="50" align="center"><h2 th:text="Name"></h2></td>
    <td height="50" align="center"><h2 th:text="Amount"></h2></td>
    <td height="50" align="center"><h2 th:text="RetailPrice"></h2></td>
    <td height="50" align="center"><h2 th:text="NetValue"></h2></td>
    <td height="50" align="center"><h2 th:text="VatRate"></h2></td>
    <td height="50" align="center"><h2 th:text="VatValue"></h2></td>
    <td height="50" align="center"><h2 th:text="Total"></h2></td>
  </tr>
  <tr bgcolor="ffffff">
    <td height="50" align="center"><h2 th:text="${productName}"></h2></td>
    <td height="50" align="center"><h2 th:text="${amount}"></h2></td>
    <td height="50" align="center"><h2 th:text="${retailPrice}"></h2></td>
    <td height="50" align="center"><h2 th:text="${productNetValue}"></h2></td>
    <td height="50" align="center"><h2 th:text="${productVatRate}"></h2></td>
    <td height="50" align="center"><h2 th:text="${productVatValue}"></h2></td>
    <td height="50" align="center"><h2 th:text="${total}"></h2></td>
  </tr>
</table>

Вопрос в том, как создать новую строку новых элементов без потери предыдущих данных? например, я хочу добиться чего-то подобного enter image description here

Пока что я все еще получаю только последний элемент из списка из-за перезаписи. Я довольно новичок в Thymeleaf, поэтому очень ценю каждую помощь и совет;)

1 Ответ

0 голосов
/ 06 мая 2018

Вместо того, чтобы помещать каждый отдельный атрибут, вы помещаете список продуктов в модель. Затем используйте th:each, чтобы зациклить его.

Контроллер

context.setVariable("products", products);

Template

<tr bgcolor="ffffff" th:each="product: ${products}">
  <td height="50" align="center"><h2 th:text="${product.product.name}"></h2></td>
  <td height="50" align="center"><h2 th:text="${product.amount}"></h2></td>
  .
  .
  .
</tr>
...