'thymeleaf - sum of objects in list inside a list
I use spring boot + thymeleaf and mySQL DB I have 3 entities:
- Category
- SubCategory
- Porducts
I would like to show the sum of products in the table
This is the code to show the sum of sub category:
<tbody>
<tr th:each="category : ${categories}">
<td th:text="${category.name}" />
<td th:text="${#lists.size(category.subCategories)}" />
</tr>
</tbody>
Solution 1:[1]
You can use collection projection and aggregate functions to accomplish this:
<tbody>
<tr th:each="category : ${categories}">
<td th:text="${category.name}" />
<td th:text="${#lists.size(category.subCategories)}" />
<td th:text="${#aggregates.sum(category.subCategories.![#lists.size(products)])}" />
</tr>
</tbody>
The expressions category.subCategories.![#lists.size(products)] produces a list of the products in subCategories, which you can just sum using #aggregates.sum.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Metroids |
