Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

scala - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...