2013-02-26 63 views
0

我有一個帶有複選框列的表單。檢票單擊複選框時檢索CheckGroup的模型

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實現這一目標?

回答

4

將AjaxFormChoiceComponentUpdatingBehavior添加到您的檢查組。每次點擊你的羣組時,ajax都會觸發。

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