我正在寻找一种在 for 循环中创建具有不同名称和值的 inputRadioGroup
的方法。
例如,我有 10 名申请人的列表。 index.scala.html 显示每个申请人的姓名,并向用户提供 3 个单选按钮(雇用、拒绝,也许)。
这就是我到目前为止所得到的。问题是,每个组都有相同的名称、值和 ID。所以我只能选择 30 个选项之一(当然,我想选择 10 个,每组 1 个)。此外,如何更改代码以便可以处理每个选择的结果?
@helper.form(action = routes.Application.save(), 'id -> "userForm") {
<fieldset>
@for(applicant <- applicants) {
<hr>
<h3>@applicant.getName()</h3>
<h4>Decision</h4>
@helper.inputRadioGroup( userForm("status"), options =
Seq("hire"->"Hire", "decline"->"Decline", "maybe"->"Maybe"), '_label ->
"Language", '_error ->
userForm("status").error.map(_.withMessage("select something")))
}
</fieldset>
<div class="actions">
<input type="submit" value="Save" class="btn btn-primary">
</div>
}
最佳答案
我找到了解决方案:最好的方法是使用自定义 HTML 输入,如下所述:https://www.playframework.com/documentation/2.4.x/JavaFormHelpers
@helper.form(action = routes.Application.save(), 'id -> "userForm") {
<fieldset>
@for(applicant <- applicants) {
<hr>
<h3>@applicant.getName()</h3>
<h4>Decision</h4>
@helper.input(userForm("status")) { (id,name,value,args) =>
<div class="radio">
<label> <input type="radio"
name="name_@applicant.getId" id="option1_id_@applicant.getId"
value="value1" @toHtmlArgs(args)>Hire
</label>
</div>
<div class="radio">
<label> <input type="radio"
name="name_@applicant.getId" id="option2_id_@applicant.getId"
value="value2" @toHtmlArgs(args)> Fire
</label>
</div>
<div class="radio">
<label> <input type="radio"
name="name_@applicant.getId" id="option3_id_@applicant.getId"
value="value3" @toHtmlArgs(args)> Maybe
</label>
</div>
}
}
</fieldset>
<div class="actions">
<input type="submit" value="Save" class="btn btn-primary">
</div>
}
关于java - 在 Play 2.4 中循环创建 InputRadioGroups,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27205073/