how can I get sum value from list in thymeleaf? - thymeleaf

I am trying to get sum of specific value inside the List<Map<String,Object>> object.
I was tried upto below code but it doesn't work.
<tr th:each="item : ${list}">
<td th:text="item.value">
</td>
</tr>
<div th:text="${#aggregates.sum(list.![value])}"></div>
How can I get sum of value from list?

Related

Parallel loops in Thymeleaf

This is my code, it does not work. I know I can not put the loops that, but how should they be to get the logic done
<tr th:each="max:${top3max}", th:each="min:${top3min}">
<td th:text="${max.getName()}"></td>
<td th:text="${min.getName()}"></td>
</tr>
As long as both Lists are the same size, you can loop through one and use the status variable to access the other. Like this:
<tr th:each="max, i: ${top3max}">
<td th:text="${max.getName()}"></td>
<td th:text="${top3min[i.index]}"></td>
</tr>
If you want something more like a traditional for loop, this will work (as long as top3max is a List -- you'll have to use .length instead of .size() if you're dealing with an array.
<tr th:each="i: ${#numbers.sequence(0, top3max.size() - 1)}">
<td th:text="${top3max[i]}"></td>
<td th:text="${top3min[i]}"></td>
</tr>

How to iterate through a list to find a match in thymeleaf?

How can I rewrite the script below with out specifying the index[x]? and allow thymeleaf to iterate the whole list to see if it find a match of test.host == server.host.
<tr th:if="${server.host == test[1].host}">
<td th:text ="${test[1].Status}"></td>
<td th:text="${test[1].host}"></td>
<td th:text="${test[1].version}"></td>
</tr>
It's not working the way I wanted it to work. It worked when I used index[x] to specify a host. I wanted it to iterate through the whole list. If test.host matches server.host. I don't want to specify an index[x]
I think you can iterate using th:each on your collection/ array
<th:block th:each="item: ${test}">///item is a var name, test is your array
<tr th:if="${server.host == item.host}">
<td th:text="${item.Status}"></td>
<td th:text="${item.host}"></td>
<td th:text="${item.version}"></td>
</tr>
</th:block>

I tried using th:remove="all-but-first" in thymeleaf but it didn't work

<td th:remove="all-but-first">
<span th:each="groups : ${userView.user.groupMemberships}">
<span th:text="${groups.group.name}"></span>;
</span>
</td>
I was trying to display the first two rows returned in th:each, so I tried displaying just the first row at first and it didn't work.
How do you display just the first row and also what should we do if we want to display just the first two or three rows in th:each?
The attribute th:remove is used for removing prototyping generated tags.
As an example:
<table th:remove="all-but-first">
<tr th:each="user : ${users}">
<td th:text="${user.name}">John Apricot</td>
</tr>
<tr>
<td>Martha Apple</td>
</tr>
<tr>
<td>Frederic Orange</td>
</tr>
</table>
This means that will remove the second and third tr, leaving only the tr used for iteration. More details here.
So if you only want to display the first element of your collection, there is no need to perform the iteration, you could simply access it by index (or even key if it's a map).
Example:
<td>
<span th:text="${userView.user.groupMemberships[0].group.name}"></span>
</td>
Displayed the first two elements of my collection and then gave a see more option to view the rest of the elements
<td>
<span th:each="groups,itrStatus : ${userView.user.groupMemberships}">
<span th:if="${itrStatus.count < 3}">
<span th:if="${itrStatus.count == 2}">,</span>
<span th:text="${groups.group.name}"></span>
</span>
<span th:if="${itrStatus.count == 3}">
, See more
</span>
</span>
</td>

JSoup to extract particular block from multiple block

I'm new to JSoup and my question here is how do I extract particular text from multiple blocks that share the same class and attributes?
For example here I want to extract the information on 3rd row of the HTML. How do I specified on my JSoup code to extract the information on 3rd row?
<tr>
<td align="center" colspan="2" class="maintitle">Active Stats</td>
</tr>
<tr>
<td class="row2" valign="top"><b>User's local time</b></td>
<td class="row1">Oct 22 2013, 07:23 PM</td>
</tr>
<tr>
<td class="row2" width="30%" valign="top"><b>Total Cumulative Posts</b></td>
<td width="70%" class="row1"><b>4</b>
<br />( 0 posts per day / 0.00% of total forum posts )
</td>
</tr>
Use the CSS-selector syntax to specify what row to select.
Element e = doc.select("tr:eq(2) td.row2").first();
System.out.println(e.text());
will result in
Total Cumulative Posts
A tip is to at least look through the Jsoup documentation before asking questions.
All this can easily be found in the API.
Jsoup - Use selector syntax

check specfic words in the records using cucumber, capybara

In my code they have one table. In that table the row is not fixed. it may added by everyone.
I that table every third column text should be "Pending". It is the condition. I dont know How to check that every third column text have "Pending".
I was trying this. I dont know weather its right or not.
page.should have_selector('tbody tr td:nth-child(3)', text: Pending)
Its my html
<table id="thisis" class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th>Default</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test1</td>
<td>true</td>
<td>
<span class="label label-success">Pending</span>
</td>
<td>
<span>View</span>
<span>/</span>
<span>Edit</span>
<span>/</span>
<span>Publish</span>
</td>
</tr>
<tr>
<td>test2</td>
<td>true</td>
<td>
<span class="label label-success">Pending</span>
</td>
<td>
<span>View</span>
<span>/</span>
<span>Edit</span>
<span>/</span>
<span>Publish</span>
</td>
</tr>
</tbody>
</table>
Thanks for your valuable answers.
Method 1: Use count
Say you have 10 rows in a page, and given your status columns have class "status". Then
expect(page).to have_css(".status", text: "Pending", count: 10)
Method 2: Use scope
To code a table with data, a convention is to assign unique id to each row at least. This will help lots of functions not only the test.
What you need to do is:
Assign an unique CSS id with data id for each row
Add a "status" class for status column for easy identifying
You view will look like this
<tr id="123-row">
<td>bla blah</td>
<td><span class="label label-success status">Pending</span>
...
</tr>
Then, for test, you can do this in Capybara:
within "##{item.id}-row .status"
expect(page).to have_content("Pending")
end

Resources