多个组件的AjaxEventBehavior相同

时间:2016-01-02 10:10:35

标签: wicket

我将图像和文本组件添加到WebMarkupContainer,如下所述:

filter.add(newFilterLabel("textSub", customerText, filtervalue));
filter.add(newFilterImage("imgSub", filtervalue));

对于每个组件,都有一个AjaxEventBehavior执行不同的操作。我想以一种方式改变它,它们可以独立于单击哪个组件来执行相同的操作。

private Component newFilterLabel(String id, IModel<String> customText,
    final SourceFilterValue currentValue) {
    final BBLabel label = new BBLabel(id, customText);
    label.add(new AjaxEventBehavior("onclick") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            doSomething(currentValue,filteredsources, target);
        }
    });
    return label;
}

private Image newFilterImage(String id, final SourceFilterValue filterValue) {
    final Image img = new Image(id, resources.getImage(EXPAND_ICON));
    img.add(new AjaxEventBehavior("onclick") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
            doSomething(img);
        }
    });
    return img;
}

您对如何更改或任何解决方法有任何建议吗?我使用Wicket 1.5.8。

1 个答案:

答案 0 :(得分:1)

AjaxBehavior只能绑定到单个元素。将其添加到层次结构中的父级,或者只让两个行为调用相同的方法:

@Override
protected void onEvent(AjaxRequestTarget target) {
    doSomething(filterValue); //filterValue has to be final to be able to access it from the inner class
}
相关问题