我正在使用play framework 2.0.4
在我的java文件中,
return ok(views.html.name.render(Name.all(),NameForm));
在我的html文件中,
@(name: List[Name],NameForm: Form[Name])
我想在@import helper中使用@select创建一个下拉列表(比如使用普通HTML中的select,option标签)来自name数组中的数据。
我是Play的新手,因此有人可以告诉我如何存档这个?
非常感谢你。
答案 0 :(得分:8)
一种方法是将选项定义为列表,由静态方法返回
创建Java类
public class ComboboxOpts {
public static List<String> myCustomOptions(){
List<String> tmp = new ArrayList();
tmp.add("This is option 1");
tmp.add("This is option 2");
tmp.add("This is option 3");
return tmp;
}
....
}
在HTML中,导入帮助程序
@import helper._
并尝试
@select(
myForm("myDropdownId"),
options = options(ComboboxOpts.myCustomOptions),
'_label -> "This is my dropdown label",
'_showConstraints -> false
)
另一种方法是定义自定义表单字段。见link
@helper.form(action = routes.Application.submit(), 'id -> "myForm") {
<select>
<option>This is option 1</option>
<option>This is option 2</option>
<option>This is option 3</option>
</select>
}
在提出这些问题之前,请务必进行广泛的Google搜索。我确信有教程和/或同样的问题已被提出
干杯
答案 1 :(得分:4)
Use String in List[String] (in your html) List<String> in your java file.
Or if you want both value and text of drop down to be different like :
<option value="1">One</option>
Use Map<String, String> instead of List<String> and pass it to @select
Java file:
Map<String, String> options = new HashMap<String, String>();
options.put("1", "One");
options.put("2", "Two");
return ok(views.html.name.render(options, NameForm));
Html:
@(name: Map<String, String>,NameForm: Form[Name])