Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of option helper in Play Framework 2.0 templates

I'm trying to use views.html.helper.select (documentation here). I don't know scala, so i'm using java. I need to pass object of type Seq[(String)(String)] to the template right? Something like:

@(fooForm:Form[Foo])(optionValues:Seq[(String)(String)])

@import helper._

@form(routes.foo){
  @select(field=myForm("selectField"),options=optionValues)
}

I don't know how to create Seq[(String)(String)] in java. I need to fill this collection with pairs (id,title) from my enum class.

Can somebody show me some expample how to use the select helper?

I found this thread on users group, but Kevin's answer didn't helped me a lot.

like image 519
UltraMaster Avatar asked Apr 12 '12 15:04

UltraMaster


1 Answers

The right type is: Seq[(String, String)]. It means a sequence of pairs of String. In Scala there is a way to define pairs using the arrow: a->b == (a, b). So you could write e.g.:

@select(field = myForm("selectField"), options = Seq("foo"->"Foo", "bar"->"Bar")) 

But there is another helper, as shown in the documentation, to build the sequence of select options: options, so you can rewrite the above code as:

@select(myForm("selectField"), options("foo"->"Foo", "bar"->"Bar")) 

In the case your options values are the same as their label, you can even shorten the code to:

@select(myForm("selectField"), options(List("Foo", "Bar"))) 

(note: in Play 2.0.4 options(List("Foo", "Bar")) doesn't compile, so you can try this options(Seq("Foo", "Bar")))

To fill the options from Java code, the more convenient way is to use either the overloaded options function taking a java.util.List<String> as parameter (in this cases options values will be the same as their label) or the overloaded function taking a java.util.Map<String, String>.

like image 115
Julien Richard-Foy Avatar answered Oct 19 '22 00:10

Julien Richard-Foy