【问题标题】:Create InputRadioGroups in a loop in Play 2.4在 Play 2.4 中循环创建 InputRadioGroups
【发布时间】:2015-01-28 01:58:00
【问题描述】:

我正在寻找一种在 for 循环中创建具有不同名称和值的 inputRadioGroups 的方法。

例如,我有一个包含 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>
}

【问题讨论】:

    标签: java html forms playframework playframework-2.0


    【解决方案1】:

    我找到了解决方案:最好的方法是使用自定义 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>
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 2016-01-20
      • 2016-08-16
      • 2020-09-16
      • 2021-12-30
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多