2012-04-24 41 views
4

我是新玩的框架(我使用Java版本)。我正在寻找一种解决方案,将选定的=“选定”添加到字段。我的代码:玩2.0 - Java:将选定的值添加到@选择字段

@select(settingsForm("projectManager"), options(projectManagers), '_label -> "Project manager") 

的HTML结果如下:

<select id="projectManager" name="projectManager"> 
    <option value="222">Henk</option> 
    <option value="96">Geert</option> 
</select> 

有谁知道@select场的规模参数添加选择=选择的HTML?我找长相结果一样:

<select id="projectManager" name="projectManager"> 
    <option value="222">Henk</option> 
    <option value="96" select="selected">Geert</option> 
</select> 

回答

2

在网上搜索了一段时间后,我的想法去了样本项目。答案在那里,就在我面前。只用视图创建表单是不够的!为了更清楚一点,只需看一下计算机数据库的例子。要创建一个空场,你只是传递一个表单对象的视图:

Controller: 
public static Result create() { 
    Form<Computer> computerForm = form(Computer.class); 
    return ok(
     createForm.render(computerForm) 
    ); 
} 

View: 
@inputText(computerForm("name"), '_label -> "Computer name") 
@inputText(computerForm("introduced"), '_label -> "Introduced date") 
@inputText(computerForm("discontinued"), '_label -> "Discontinued date") 

如果要填写表单,您需要将数据传递给表单。如示例所示:

public static Result edit(Long id) { 
    Form<Computer> computerForm = form(Computer.class).fill(
     Computer.find.byId(id) 
    ); 
    return ok(
     editForm.render(id, computerForm) 
    ); 
} 
0

其实,我不知道答案,但根据source code必须通过某种方式修改projectManagers

此外,您可以尝试在Play-Framework group中提出此问题。

也许它会对你有帮助。

0

我假设projectManager是某种关系。然后,你可以尝试:

@select(
    settingsForm("projectManager.id"), 
    options(projectManagers), 
    '_label -> "Project manager" 
) 

@see:similar case

+0

Projectmanager是一个带键值 - > id,名称的Hashmap。我确实从其他程序中提取这个列表。我的模型只包含一个字符串(即是id): \t @Column(name =“project_manager” \t私人字符串projectManager; //可以像“96” 当projectManager充满我要的是,选择框的选定值由projectManager值选择,如果projectManager为'96',那么'Geert'将被选中 – Jacob 2012-04-26 14:36:37

2

在Scala中,我知道在控制器下面的代码将使96选择

settingsForm.bind(Map("projectManager" -> "96")) 

在Java中它应该工作,从我的猜测一样。

+1

使用bind的麻烦在于它会导致错误消息的任何必填字段的值没有提供这似乎是最好的Scala解决方案:@select(settingsForm(“projectManager”)。copy(value = Some(“96”)), options(projectManagers),'_label - >“Project manager”) – Joffer 2012-12-23 03:03:04

+0

Yep。 .. @ Joffer对钱是正确的。这种方法解决了一个问题,但导致了ano疗法。 – bitstream 2014-04-07 14:02:29

相关问题