【问题标题】:play framework bind form field to checkbox播放框架将表单字段绑定到复选框
【发布时间】:2014-12-05 03:53:57
【问题描述】:

如何将 myForm 中名为“isMarried”的字段绑定到视图中的复选框控件?我想要这种双向绑定。我正在使用来自https://github.com/nostalgiaz/bootstrap-switch的引导开关

@(myForm: Form[_])
@helper.form(action = controllers.index.submit, 'id -> "myForm") {

<fieldset>
<legend>General information</legend>
<div class="twipsies well">
   <div class="control-group">
        <label class="control-label" for="isMarried">Married</label>
        <div class="controls">
            <div class="switch switch-small" tabindex="0" data-on-label="Yes" data-off-label="No" data-off="warning">
                <input id="isMarried" name="isMarried" type="checkbox"/>
            </div>
        </div>
    </div>

</div>

</fieldset>
}

【问题讨论】:

  • 抱歉,我无法理解您的问题。 “双向”是什么意思?而且我不知道您的问题与引导开关有关。
  • 双向可能意味着(a)复选框被初始化为域对象中的值,并且(b)当提交表单时,域对象被更新以反映复选框的状态.

标签: data-binding twitter-bootstrap playframework playframework-2.0


【解决方案1】:

我没有使用复选框(在 Pay 中),但我会使用复选框助手:

例子:

@checkbox(field = myForm("done"))

查看来源:https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/views/helper/checkbox.scala.html

如果您需要自定义 HTML 以满足您的需求,请编写您自己的帮助程序/标签:

祝你好运!

【讨论】:

  • FWIW,这是我的工作复选框代码:@checkbox(myForm("myProperty"), '_label -> Messages("my.message.code"))
【解决方案2】:

型号是:

/**

 * This class represent a school model 

 * @author Giovanni Alberto García

 * @version 1.0

 */

@Entity

public class School extends Model {


  @Id

  @Column(unique=true, nullable=false)

  public Long id;


  @ManyToMany(cascade = CascadeType.ALL)

  public List<User> members;


  public boolean rElecCFE;

...

}

如您所见,rElecCFE 是布尔值,因此视图如下:

@(schoolForm: Form[School])(schoolId: Long)

@import helper._ 

@main("Completar registro") {

<div id="edition">

  @form(action=routes.Nama.doEdit()) {
  <fieldset id="registration-electicity">
    <input type="hidden" name="id" value="@schoolId"/>
    <legend>Electricidad</legend>
    @checkbox(schoolForm("rElecCFE"),
    '_showConstraints->false,
    '_disabled->""
    )
...
  }
</div>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多