自动将其更改值提交给域对象的 Wicket 复选框
我可以使复选框自动提交其在 Wicket 中所属的表单的最简洁方法是什么?我根本不想包含提交按钮。该复选框由域对象(本例中为“帐户”)中的布尔字段支持。
省略不相关部分的简化示例:
EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id);
PropertyModel<Boolean> model = new PropertyModel<Boolean>(accModel, "enabled");
CheckBox checkBox = new CheckBox("cb", model);
Form form = new Form("form");
form.add(checkBox);
add(form);
HTML:
<form wicket:id="form" id="form" action="">
<input wicket:id="cb" type="checkbox" />
</form>
编辑:为了澄清,我的目标只是在切换复选框时更改域对象的字段(->数据库中的值)。任何(干净、简单)的方法来实现这一点都可以。 (我不确定您是否真的需要该表格。)
What's the cleanest way I can make a checkbox automatically submit the form it belongs to in Wicket? I don't want to include a submit button at all. The checkbox is backed by a boolean field in a domain object ("Account" in this case).
Simplified example with irrelevant parts omitted:
EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id);
PropertyModel<Boolean> model = new PropertyModel<Boolean>(accModel, "enabled");
CheckBox checkBox = new CheckBox("cb", model);
Form form = new Form("form");
form.add(checkBox);
add(form);
HTML:
<form wicket:id="form" id="form" action="">
<input wicket:id="cb" type="checkbox" />
</form>
Edit: To clarify, my goal is just to change the domain object's field (-> value in database too) when the checkbox is toggled. Any (clean, easy) way to achieve that would be fine. (I'm not sure if you actually need the form for this.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需覆盖复选框的wantOnSelectionChangedNotifications()——即使不覆盖onSelectionChanged()——似乎就可以做到我想要的。
这样,您就不需要 Java 端的表单,因此上面的代码将变为:
随意添加更好的解决方案,或者更好地解释这种方法的情况!
编辑:经过仔细检查,我猜该方法的 Javadoc 相当清楚地说明了为什么这会达到我想要的效果(强调我的):
Just overriding wantOnSelectionChangedNotifications() for the checkbox—even without overriding onSelectionChanged()—seems to do what I want.
This way you don't need the form on Java side, so the above code would become:
Feel free to add better solutions, or a better explanation of what's going on with this approach!
Edit: On closer inspection, I guess the method's Javadoc makes it reasonably clear why this does what I wanted (emphasis mine):
虽然这可能有效,但您最好使用 AjaxCheckBox 。可以连接匿名子类以立即接收事件以及对复选框本身外部的 UI 进行更改。
在这个人为的示例中,WebMarkupContainer 将与复选框的值同步可见。
While this may work, you are far better off using AjaxCheckBox. An anonymous subclass can be wired to receive events immediately as well as make changes to the UI outside the checkbox itself.
In this contrived example, the WebMarkupContainer would be made visible in sync with the value of the checkbox.