【问题标题】:How to synchronize an Ajax onClick and onUpdate Event如何同步 Ajax onClick 和 onUpdate 事件
【发布时间】:2015-12-11 10:18:55
【问题描述】:

我有一个表格来显示所选用户的权限。在列中我有角色,在行中我有函数。在表格中的每个单元格中,我都有一个 Wicket AjaxCheckBox,它使用 onUpdate 事件来触发操作。但除此之外,管理员还应该能够单击该行以选中复选框。因此,每个单元格都附加了一个 onClick 事件。

如果我单击 CheckBox,它不会选择,但如果我双击它会。由于我一键触发了单元格的 onClick 和 CheckBox 的 onUpdate ,因此它会自行中和。

我正在运行 Wicket 6.20

现在我是这样运行的:

final WebMarkupContainer cell = new WebMarkupContainer("cont");
cell.setOutputMarkupId(true);
cell.setOutputMarkupPlaceholderTag(true);
cell.setVisibilityAllowed(true);
cell.add(AttributeModifier.append("class", checkState));

cell.add(new AjaxEventBehavior("onclick") {
    private static final long serialVersionUID = 1L;
        @Override
        protected void onEvent(AjaxRequestTarget target) {
            eventHandler(checked, checkState);
            target.add(cell);
        }
});

item.add(cell);
cell.add(new AjaxCheckBox("checkbox", checked) {
    private static final long serialVersionUID = 1L;
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        eventHandler(checked, checkState);
        target.add(cell);
}});

public void eventHandler(IModel<Boolean> checked, IModel<String> checkState) {
    boolean isChecked = checked.getObject();
    if (isChecked == false) {
        checked.setObject(true);
        checkState.setObject("checked");
        //Do sth
    } else if (isChecked == true) {
        checked.setObject(false);
        checkState.setObject("unchecked");
        //Do sth
    }
}

【问题讨论】:

    标签: ajax checkbox wicket


    【解决方案1】:

    这是 Wicket 7 吗?然后你应该防止你的 AjaxCheckBox 上的事件冒泡:

    @Override
    protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
    {
        super.updateAjaxAttributes(attributes);
    
        attributes.setEventPropagation(EventPropagation.STOP);
    }
    

    【讨论】:

    • 它仍然是 Wicket 6.20,但我会检查它是否有效
    • 迁移6->7很流畅,比1.5->6好很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2021-03-05
    • 2021-02-18
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多