I use spring boot + thymeleaf and mySQL DB
I have 3 entities:
Category
SubCategory
Porducts
The current table
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>
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.
Related
I have requirement something similar like below in our application,
I would like to show the below details in the form of table.
Country Name Population CapitalCity Aria
US XX XX XX XX
IN YY YY YY YY
User can choose in the configuration page which columns he is interested to see.
In the backend I set model attributes as below(using spring MVC),
model.addAttribute("selColumns", "column keys");
model.addAttribute("countryDetails", "List of country details");
In the CountryDetail class, field names and selColumns key names are same.
class CountryDetails {
private String country,
population,
CapitalCity,
Aria;
}
In the UI I am trying with the following code to achieve the same.
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<c:forEach items="${selColumns}" var="item">
<th><spring:message code="${item}" /></th>
</c:forEach>
</tr>
</thead>
<tbody>
<c:forEach items="${countryDetails}" var="det">
<tr>
<c:forEach items="${selColumns}" var="item">
<td>
//Below code is not working
<c:out value="${item.det}" /></td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
Table header is working fine. But I am struggling to show row information only for the configured columns.
Code wouldn't work because it tries to find getDet() at the java side.
Could some one please help is there any way in JSP, if I give property(field) name the corresponding value would return?
Something like this
Thanks in advance,
kitty
Try this one.
<tbody>
<c:forEach items="${countryDetails}" var="det">
<tr>
<c:forEach items="${selColumns}" var="item">
<td>
<c:out value="${det[item]}" />
</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
Note:
selColumns = ["Country", "Population", "CapitalCity", "Aria"]
CountryDetails properties = Country,Population,CapitalCity,Aria
I got this code:
<th:block th:if="${!#lists.isEmpty(partyInfoByUploaderList)}" th:each="pInfo : ${partyInfoByUploaderList}">
<h4 th:text="${pInfo.getCharName()}"></h4>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th:block th:if="${!#lists.isEmpty(pInfo.getCharColumnTitles())}" th:each="title: ${pInfo.getCharColumnTitles()}">
<td th:text="${title}"></td>
</th:block>
</tr>
</thead>
<tbody>
<tr>
<th:block th:each="val: ${pInfo.getCharColumnValues()}">
<td th:text="${val}"></td>
</th:block>
</tr>
</tbody>
</table>
And I got this result:
http://screenshot.ru/6dfbe559f905a17dff44d360b5d13f28
So this code correctly create table head, and dont parse table body.
BTW, after delpoy this code appear at another place:
<th:block th:each="val: ${pInfo.getCharColumnValues()}">
<td th:text="${val}"></td>
</th:block>
Web source code in browser:
http://screenshot.ru/9d3fab8030c5ca382ab81094e0c1ca86
Question #2:
Why this code produce TemplateProcessingException error?
P.S. Not enough reputation for images, so just links. Soz
According to Thymeleaf Documentation, <th:block></th:block> should wrap <tr></tr> and not the opposite. So you should try this :
<th:block th:each="val: ${pInfo.getCharColumnValues()}">
<tr>
<td th:text="${val}"></td>
</tr>
</th:block>
I want to create the row as per the list i am receiving using xslt and css. Currently my xslt looks like below. In the below code i am reading the DataList using xsl:for-each and I want to create a new row if my data list size is more then 7. Could anyone please help on this.
<table width="100%">
<tr>
<td width="97px" style="font-size:14px;font-weight:bold;color:#9EB7B4;">DATA:</td>
<td>
<table style = "width:100%; ">
<tr style="height:35px">
<xsl:for-each select="DataList">
<td><a href="#{generate-id(DataName)}" ><xsl:value-of select="DataName"/></a></td>
</xsl:for-each>
</tr>
</table>
</td>
</tr>
</table>
I am trying to understand how to loop through all entries in a Map in Thymeleaf. I have a domain object being processed by Thymeleaf that contains a Map.
How do I loop through the keys and fetch the values ?
Thanks.
Nevermind... I found it...
<tr th:each="instance : ${analysis.instanceMap}">
<td th:text="${instance.key}">keyvalue</td>
<td th:text="${instance.value.numOfData}">num</td>
</tr>
Thanks.
In case you have a List as the value. For example, when you have a map with key being the category, and value being a list of items pertaining to that category, you can use this:
<table>
<tr th:each="element : ${catsAndItems}">
<td th:text="${element.key}">keyvalue</td>
<table>
<tr th:each="anews : ${element.value}">
<td th:text="${anews.title}">Some name</td>
<td th:text="${anews.description}">Some name</td>
<td th:text="${anews.url}">Some name</td>
<td th:text="${anews.logo}">Some name</td>
<td th:text="${anews.collectionDate}">Some name</td>
</tr>
</table>
</tr>
</table>
Can I make the table with rules in my aspx page.
right now I am dispalying the page some thing like this.
Benefit Type : 000
Benfit Set: BCPCP
Converage Level : IND -Individual
Deductable Type : D-DED
Can I differnciate with the rules each and every row. like this
Benefit Type : 000
Benfit Set: BCPCP
Converage Level : IND -Individual
Deductable Type : D-DED
here is my Aspx code.. is there any way I can make like this?
<table>
<tr>
<td>
</td>
</tr>
You can just do a row with an hr in it like this: http://jsfiddle.net/WXSpD/
<table>
<tr>
<td>Benefit Type:</td>
<td>000</td>
</tr>
<tr>
<td colspan="2"><hr/></td>
</tr>
<tr>
<td>Benfit Set:</td>
<td>BCPCP</td>
</tr>
</table>
Your question is very hard to understand, but if you're just looking for a two-by-two table, can't you do it like this?:
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
If there's something else you're looking for, can you clarify a little bit?