Data in templates

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Data in templates

rafa189
Hi,

In our Spring application we are using Thymeleaf templates. On a results page we have a list of records, but one field should not be repeated (outside of the th:each loop) so that it appears only once.

How can this be achieved?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Data in templates

banker
Could you present a small example?

Possible option is create a bean with utility methods that transform the list into the records you want and loop over that instead.

Example: https://stackoverflow.com/questions/45707545/how-to-use-custom-spring-el-function-in-thymeleaf
Reply | Threaded
Open this post in threaded view
|

Re: Data in templates

rafa189
Thanks for your feedback!

What I am trying to achieve, i.e.

On the result page:

You searched for: >>Mercedes-Benz E250<< <-- (Header - ONLY ONCE) is within the th:each tag but should be displayed only once.

Results list:

Offer 1
Offer 2
Offer 3
...

Reply | Threaded
Open this post in threaded view
|

Re: Data in templates

Metroids
Yes, you can accomplish that with something like this:

<div th:each="item, status: ${list}">
    <h1 th:if="${status.first}" th:text="|You searched for ${search}|" />
    <div th:text="${item}" />
</div>

You can use the iterator status variable to ask if it's the first item in the list.

I would still recommend you put header outside the th:each, if possible.

Reply | Threaded
Open this post in threaded view
|

Re: Data in templates

rafa189
Thanks for the code. I will give it a try.

Cheers!