Vitaly Olegovitch
Vitaly Olegovitch

Reputation: 3547

Play: populating a helper select from Java

I have to populate an HTML select with ids and labels. I also need the labels to be ordered alphabetically.

If I pass a List<String> of labels, I lose ids. If I pass a Map<String,String>, I have ids, but the ordering is not kept.

My page:

@(countries: Map[String,String], myForm: Form[JUG], title: String) @header(title)
...
@helper.select(myForm("countryId"), helper.options(countries) )

How can I populate the HTML select with ids and labels alphabetically ordered?

I have to use the helper to keep the selected element between requests.

Upvotes: 2

Views: 380

Answers (1)

gtgaxiola
gtgaxiola

Reputation: 9331

If you use LinkedHashMap as the implementation of Map you may keep the order of how objects were inserted.

Shouldn't matter if using Scala's or Java's implementation.

This class implements mutable maps using a hashtable. The iterator and all traversal methods of this class visit elements in the order they were inserted.

Upvotes: 1

Related Questions