自动将其更改值提交给域对象的 Wicket 复选框

发布于 2024-10-03 18:56:47 字数 687 浏览 6 评论 0原文

我可以使复选框自动提交其在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-10-10 18:56:47

只需覆盖复选框的wantOnSelectionChangedNotifications()——即使不覆盖onSelectionChanged()——似乎就可以做到我想要的。

这样,您就不需要 Java 端的表单,因此上面的代码将变为:

EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id);

add(new CheckBox("cb", new PropertyModel<Boolean>(accModel, "enabled")){
    protected boolean wantOnSelectionChangedNotifications() {
        return true;
    }
});

随意添加更好的解决方案,或者更好地解释这种方法的情况!

编辑:经过仔细检查,我猜该方法的 Javadoc 相当清楚地说明了为什么这会达到我想要的效果(强调我的):

如果为真,则
每次都会生成往返
选择更改,导致
模型正在更新
(仅此
组件)和 onSelectionChanged
被召唤。

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:

EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id);

add(new CheckBox("cb", new PropertyModel<Boolean>(accModel, "enabled")){
    protected boolean wantOnSelectionChangedNotifications() {
        return true;
    }
});

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):

If true, a
roundtrip will be generated with each
selection change, resulting in the
model being updated
(of just this
component) and onSelectionChanged
being called.

一人独醉 2024-10-10 18:56:47

虽然这可能有效,但您最好使用 AjaxCheckBox 。可以连接匿名子类以立即接收事件以及对复选框本身外部的 UI 进行更改。

final WebMarkupContainer wmc = new WebMarkupContainer("wmc"); 
final EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id); 
wmc.setVisible(false); 
wmc.setOutputMarkupPlaceholderTag(true);
form.add(new AjaxCheckBox("cb", new PropertyModel<Boolean>(accModel, "enabled")) {
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        wmc.setVisible(accModel.isEnabled());
        target.addComponent(wmc);
        // .. more code to write the entity
    }
});

在这个人为的示例中,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.

final WebMarkupContainer wmc = new WebMarkupContainer("wmc"); 
final EntityModel<Account> accModel = new EntityModel<Account>(Account.class, id); 
wmc.setVisible(false); 
wmc.setOutputMarkupPlaceholderTag(true);
form.add(new AjaxCheckBox("cb", new PropertyModel<Boolean>(accModel, "enabled")) {
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        wmc.setVisible(accModel.isEnabled());
        target.addComponent(wmc);
        // .. more code to write the entity
    }
});

In this contrived example, the WebMarkupContainer would be made visible in sync with the value of the checkbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文