Wicket在单击复选框时检索CheckGroup的模型

时间:2013-02-26 10:09:17

标签: java ajax web-applications wicket wicket-1.5

我有一个带有复选框列的表单。

ArrayList<MyObject> myObjectsList = // Load from database ...
ArrayList<MyObject> selectedObjects = new ArrayList<MyObject>();

Form form = new Form("my-form");

CheckGroup<MyObject> myObjectGroup = new CheckGroup<MyObject>("object-check-group", new AbstractReadOnlyModel() {
  @Override
  public Object getObject() {
    return selectedObjects;
  }
});

WebMarkupContainer container = new WebMarkupContainer("table-container");

DataView dataView = new DataView("table-body", new ListDataProvider<MyObject>(myObjectsList)) {
  public void populateItem(final Item item) {
    final MyObject myObject = (MyObject) item.getModelObject();
    item.add(new Check("check-box", item.getModel())); // Checkbox for each item
    // And a couple more columns
  }
};

container.add(dataView);
group.add(container);
form.add(group);
add(form);

现在我想要的是每当所选对象的数量发生变化时更新表单:

item.add(new Check("check-box", item.getModel()).add(new AjaxEventBehavior("onclick") {
  @Override
  protected void onEvent(AjaxRequestTarget target) {
    selectedObjects = (ArrayList<MyGroup>) myObjectGroup.getModelObject(); // this does not work sadly :(
    // Update form elements
  }
}));

使用Wicket实现这一目标是否容易?

1 个答案:

答案 0 :(得分:4)

将AjaxFormChoiceComponentUpdatingBehavior添加到您的支票组。每次单击组中的检查时,ajax都会触发。

    myObjectGroup.add(new AjaxFormChoiceComponentUpdatingBehavior() 
    {
        @Override
        protected void onUpdate(AjaxRequestTarget target) 
        {
            //update form elements  
        }         
    });