java - 在 Play 2.4 中循环创建 InputRadioGroups

标签 java html forms playframework playframework-2.0

我正在寻找一种在 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/

相关文章:

html - HTML5错误验证中表格宽度边框cellspacing cellpadding全0

php - 从 TeSTLink 按下保存按钮时未触发 onClick

Angular2/4/5 - 响应式表单,你应该使用 AbstractControl 还是 FormControl?

java - Primefaces selectOneListbox 验证错误 : Value is not valid

java - 是否可以重构此 Java 代码?

java - 带基数排序的 NullPointerException

javascript - 突出显示空输入字段

java - 使用缺少字段的 jackson 反序列化json

php - 传递从 mysql 表收集的下拉数据

php - 如何在 CURLOPT_POSTFIELDS 中包含数组数据?