Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a collection in thymeleaf th:each using another property in comparison

I am trying to filter the collection using Thymeleaf by following the example in the following url. "Projection & selection on collection" section. http://doanduyhai.wordpress.com/2012/04/14/spring-mvc-part-iv-thymeleaf-advanced-usage/

<tr th:each="artist,rowStat : ${listArtits.?[alive == true]}">
...
</tr>

However I would like to use another property instead of fixed value (true/false). For example

<tr th:each="artist,rowStat : ${listArtits.?[played > playedCountReq]}">
...
</tr>

where as playedCountReq is another form variable available to Thymeleaf. I get the following error. Property or field 'playedCountReq' cannot be found on object of type ...

I tried multiple ways but no success. Any suggestions?

like image 974
gmansoor Avatar asked Oct 20 '14 23:10

gmansoor


People also ask

How do you use attributes in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages . This is a Spring EL expression.

How do I use the Thymeleaf th object?

We use th:action to provide the form action URL and th:object to specify an object to which the submitted form data will be bound. Individual fields are mapped using the th:field=”*{name}” attribute, where the name is the matching property of the object.


1 Answers

I succeded :) Here is solution:

in controller:

(...)
Person p1 = new Person();
p1.setAge(20);
Person p2 = new Person();
p2.setAge(30);
List<Person> list = Lists.newArrayList(p1,p2);
modelMap.addAttribute("list", list);
Integer minAge = 13;
modelMap.addAttribute("minAge", minAge);
(...)

in html:

<table th:with="min=${minAge}">
<tr th:each="person,rowStat : ${list.?[age > __${min}__]}">
<td><span th:text="${person.age}"></span></td>
</tr>
</table>

Output:

30

Hope this help

like image 53
Iwo Kucharski Avatar answered Sep 23 '22 03:09

Iwo Kucharski