在Play Framework 2.0模板中使用选项帮助器 [英] Use of option helper in Play Framework 2.0 templates
问题描述
我正在尝试使用views.html.helper.select
(文档此处).我不知道scala,所以我正在使用java.我需要将Seq [(String)(String)]类型的对象传递给模板,对吗?像这样:
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)
}
我不知道如何在Java中创建Seq [(String)(String)].我需要用我的枚举类中的对(id,title)填充此集合.
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?
我在用户上发现了此线程组,但是凯文(Kevin)的回答对我没有多大帮助.
I found this thread on users group, but Kevin's answer didn't helped me a lot.
推荐答案
正确的类型是:Seq[(String, String)]
.它表示成对的String序列.在Scala中,有一种使用箭头定义对的方法:a->b == (a, b)
.因此,您可以编写例如:
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"))
但是,如文档中所示,还有另一个助手来构建选择选项的序列:options
,因此您可以将上面的代码重写为:
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")))
(注意:在Play 2.0.4中,options(List("Foo", "Bar"))
无法编译,因此您可以尝试使用此options(Seq("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>
.
这篇关于在Play Framework 2.0模板中使用选项帮助器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!