Stop changing list-item order

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

Stop changing list-item order

luchev
Hello,
I'm trying to display list-item name. I've got a dvd-media list. Each dvd-media has a countriesList (Set of countries)
object inclusion

I've got a controller sending a List called "dvds".
With thymeleaf forEach function, I succeed displaying countries name but thymeleaf change list-items order when I refresh the page.

First, it display: "country1Name", "country2Name", "country3Name".
After refreshing (F5), it display: "country2Name", "country1Name", "country3Name".

<div class="row" th:each="dvd : ${dvds}">
    <div class=col-3>
         <div th:each="country, iterStat : ${dvd.countries}" th:text="${!iterStat.last} ? ${country.name} + ', ': ${country.name}"></div>
    </div>
</div>
If i add "iterIndex" property, I can see that countryNames doesn't have the same index between every refresh.

Why is there any logic for displaying list items in a for each tag?

Happy new year to everyone!
Reply | Threaded
Open this post in threaded view
|

Re: Stop changing list-item order

Reiju
TreeSet of countries?
Reply | Threaded
Open this post in threaded view
|

Re: Stop changing list-item order

luchev
Hi,
I tried replacing Set with TreeSet:

In Dvd.class:
    @ManyToMany(...)
    private TreeSet<Country> countries = new TreeSet<>();
In Country.class:
    @ManyToMany(...)
    private TreeSet<Dvd> dvds = new TreeSet<>();
But this raise an annotation exception when building:
Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.keziko.dvdteka.entities.Dvd.countries

Is the treeSet syntax correct?

You're right in the fact it's a collection's handling problem.
Reply | Threaded
Open this post in threaded view
|

Re: Stop changing list-item order

luchev
I corrected TreeSet syntax:
private SortedSet<Country> countries = new TreeSet<>();

Now, I'm trying to implement Comparable interface and its method CompareTo in the Country Class to respect natural order of countries. Don't know how to do that for the moment.
Reply | Threaded
Open this post in threaded view
|

Re: Stop changing list-item order

luchev
Finally, I had to
- replace Set<Country> with TreeSet<Country>
- implement Comparable interface and its CompareTo method  into Country Class:

Within Dvd.class:
private SortedSet<Country> countries = new TreeSet<>();

Within Country.class:
public class Country implements Comparable<Country>
    @Override
    public int compareTo(Country o) {
        int numb = (this.id).intValue() - (o.id).intValue();
        return numb;
    }

Now it works!

Thanks Reiju for replying!