4

我是游戏框架的新手(我使用 Java 版本)。我正在寻找一种将 selected="selected" 添加到字段的解决方案。我的代码:

@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 字段的 scale 参数以将 select=selected 添加到 html 中?我正在寻找的结果如下:

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

4 回答 4

2

在网上搜索了一段时间后,我想到了示例项目。答案就在那里,就在我面前。仅创建带有视图的表单是不够的!为了更清楚地说明这一点,请看一下计算机数据库示例。要创建一个空字段,只需将一个 Form 对象传递给视图:

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)
    );
}
于 2012-05-01T12:58:40.867 回答
2

在 scala 中,我知道控制器中的以下代码将96选中

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

在 Java 中,它的工作方式应该与我的猜测相同。

于 2012-09-27T21:45:00.327 回答
0

实际上,我不知道答案,但是根据源代码,您必须以某种方式修改您的projectManagers

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

也许它会对你有所帮助。

于 2012-04-25T17:17:35.393 回答
0

我假设 projectManager 是某种关系。那你可以试试:

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

@see:类似情况

于 2012-04-25T21:01:58.937 回答