2

我有一个包含 ManyToMany 关系的模型:

@JoinTable(name = "Contract_has_Institution", joinColumns = { @JoinColumn(name = "contract_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "institution_id", referencedColumnName = "id") })
@ManyToMany
public List<Institution> institutionCollection;

在 scala.html 我有两个选择,在列表中添加两个元素(两个机构)

    @select(contractForm("institutionCollection"),
         options = options(InstitutionController.options),
         '_default -> "--- Choose an institution ---",
         '_label -> "Institution A",
         '_showConstraints -> false)

    @select(contractForm("institutionCollection"),
         options = options(InstitutionController.options),
         '_default -> "--- Choose an institution ---",
         '_label -> "Institution B",
         '_showConstraints -> false)

我试过将 [] 添加到这样的字段中

 contractForm("institutionCollection[]"),

但它不起作用。如何将两个选定的选项绑定到列表?

4

1 回答 1

2

在您看来,如果您使用当前代码:

 @select(contractForm("institutionCollection"),
     options = options(InstitutionController.options),
     '_default -> "--- Choose an institution ---",
     '_label -> "Institution A",
     '_showConstraints -> false)

@select(contractForm("institutionCollection"),
     options = options(InstitutionController.options),
     '_default -> "--- Choose an institution ---",
     '_label -> "Institution B",
     '_showConstraints -> false)

因此,您可以使用以下代码获取多项选择的值(这只是示例):

public static Result someAction() {
    Map<String,String[]> formData = request().body().asFormUrlEncoded();

    Institution institution;
    // get all institutionCollection select value
    for (String insId : formData.get("institutionCollection")) {
        Logger.debug("INSTITUTION SELECTED = " + insId); // log info

        // I assume that select value is the ID of each Institution record
        institution = Institution.find.byId(Long.parseLong(insId))
        SomeModel.institutionCollection.add(institution) // add to the list to your model
    }
    SomeModel.save(); // save changes
    return ok("success");
}
于 2013-05-07T02:33:40.217 回答