Reputation: 3547
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 id
s. If I pass a Map<String,String>
, I have id
s, 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 id
s and labels alphabetically ordered?
I have to use the helper to keep the selected element between requests.
Upvotes: 2
Views: 380
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