How to filter a collection in thimelaf th: each uses a different property in comparison

I am trying to filter a collection using Thymeleaf, following the example in the following URL. Section "Projection and selection in the collection." 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 a different property instead of a fixed value (true / false). for instance

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

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

I tried several ways but did not succeed. Any suggestions?

+6
source share
2 answers

I succeeded :) Here is the solution:

in the 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 for this help

+5
source

Within the selection filter, unqualified properties refer to an element of the filtered collection. But the #root variable #root always defined as the root context object. Here's how you can reference variables at the root level from a selection filter.

 <tr th:each="artist,rowStat : ${listArtists.?[played gt #root.playedCountReq]}"> โ€ฆ </tr> 

For more information, see # thisis and #root variables "in the Spring EL documentation.

0
source

Source: https://habr.com/ru/post/977005/


All Articles