21

我正在尝试使用views.html.helper.select此处的文档)。我不知道scala,所以我正在使用java。我需要将 Seq[(String)(String)] 类型的对象传递给模板,对吗?就像是:

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

@import helper._

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

我不知道如何在 java 中创建 Seq[(String)(String)]。我需要用我的枚举类中的对 (id,title) 填充这个集合。

有人可以告诉我一些如何使用选择助手的例子吗?

我在用户组上找到了这个帖子,但凯文的回答对我没有多大帮助。

4

1 回答 1

40

正确的类型是:Seq[(String, String)]. 它意味着一个字符串对的序列。在 Scala 中,有一种使用箭头定义对的方法:a->b == (a, b)。所以你可以写例如:

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

但是还有另一个帮助器,如文档中所示,用于构建选择选项序列:options,因此您可以将上述代码重写为:

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

如果您的选项值与其标签相同,您甚至可以将代码缩短为:

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

(注意:在 Play 2.0.4options(List("Foo", "Bar"))中无法编译,所以你可以试试这个options(Seq("Foo", "Bar"))

要从 Java 代码中填充选项,更方便的方法是使用options采用 ajava.util.List<String>作为参数的重载函数(在这种情况下,选项值将与其标签相同)或采用java.util.Map<String, String>.

于 2012-04-13T07:58:46.520 回答