Thymeleaf - Iterate Object list based on an object's property

nmy

I have a product object list and I want to iterate it in HTML page based on some conditions. I want to iterate this only for the products, which product type is 'BAR'. I have done this as follows.

<th:block th:if="${#strings.isEmpty(foo.destination)}" >
    <div  th:each ="product, prodStat:${foo.productList}" th:if="${product.type eq 'BAR'}" th:with="bar=${product}">                                   
        <div th:text="${bar.cityName}">London</div>                              
    </div>
</th:block>

But now I want product list to iterate only for the first 5 'BAR' products only. How can I achieve this?

Andrew

You can first use the "SpEl Collection Selection" syntax to filter your productList to only elements matching the type "BAR". Then you can use the iteration status prodStat to only display the first 5 elements. Like so:

<th:block th:if="${#strings.isEmpty(foo.destination)}" >
    <div th:each="product, prodStat:${foo.productList.?[#this.type eq 'BAR']}" 
         th:if="${prodStat.index} lt 5" 
         th:with="bar=${product}">                                   
        <div th:text="${bar.cityName}">London</div>                              
    </div>
</th:block>

In the above you can see the iteration is now performed over foo.productList.?[#this.type eq 'BAR'], this is a filtered version of productList containing only the elements with the type (referenced using #this.bar) equalling 'BAR'.

The number of iterations is then limited using th:if and the iteration status prodStat.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterate over object's property

Thymeleaf - conditional rendering based on value of object property

How to Sort List based on Inner Object's property in java 8

Collector to create a List of List based on a property of the object

In a list, it is possible to obtain the object type of an object based on a object property in Typescript?

how to iterate through an object's list?

How to iterate through the row property of the sheet object and create a comma delimited list of row id's?

convert list of items to ranges based on object property

Generate a list with a head based on object property in array

How to filter an object list based on an unknown property

Iterate through list objects to find object with matching property value

Get object type based on it's property values

How to not serialize an object based on a property's value?

Get the latest Object from List based on one property in the object

sorting a list of object based on the property date sudo object values

Looping through objects then iterate through the object's property and value

How to sort a JS object list based on a property when the property is not consistent

JSON object's property value to list

Thymeleaf display field of object of a list

How to bind an object list with thymeleaf?

Form for each object in list - ThymeLeaf

"Exception evaluating SpringEL expression" error while trying to iterate List(<Object>) in Thymeleaf Spring Boot

Remove first matching object by property in a list based on another list

Filter JSON object list based on property value with property value is in another JSON object list

Thymeleaf - Setting Object's Field To Another Object

How to iterate through a C# object's properties and update the corresponding property on another object

Find property in an object then adding an item to it's list property

Replace object property in list

Get index of object in Generic/List based on value of a property

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive