Wicket CheckGroup Ajax duplicities in check group model

737 Views Asked by At

I have a simple panel (the panel itself is part of bigger form) with CheckGroup. The checkboxes in the check group are generated in the list view component. I need to dynamicaly change this panel and upon every change I need to retrieve the selected items. The code of the panel basically looks like this:

CheckGroup<MyObject> group = new CheckGroup<MyObject>(ID, selectedObjects);
ListView<MyObject> objectList = new ListView<MyObject>(ID, values) {

    @Override
        protected void populateItem(ListItem<MyObject> item) {
            Check<MyObject> check = new Check<MyObject>(TIME_CHECK, item.getModel());
            Label l = new Label(TIME_LABEL, item.getModel());

            item.add(check);
            item.add(l);
        }
}
group.add(objectList);
group.add(new AjaxFormChoiceComponentUpdatingBehavior() {

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            System.out.println("Selected objects: "+selectedObjects.size());
        }
    });
add(group);

Now, the problem is, whenever I click on the check box, the two identical objects are added to the selectedObjects list. And if I remove the AjaxFormChoiceComponentUpdatingBehavior, no objects are added to the list (which make sense, because I'm not submiting the form at this point).

I'm not exactly sure how to solve this problem and the best solution I came up with was getting the list and the going through it, removing duplicities.

Also, sorry for the title, but I have no idea how to name this problem.

Here's a little example to clarify the problem: Lets say the check group is displaying these objects:

object 1
object 2
object 3
object 4
object 5

Then when I select object 1 the model of check group (=selectedObjects) will look like this:

object 1
object 1
3

There are 3 best solutions below

0
On

This was a bug on wicket. I used version 7.1 then changed on version 7.10 and the problem solved!

0
On

Try

target.add(form);`

or

target.add(group);

or something similar, depending on your code.

0
On

You can try the following construction, recreate the checkgroup so that it is up to date. Maybe a bit overkill, but it should do the trick.

   private CheckGroup<MyObject> group; 
   private IModel<MyObject> selectedObjects;


   public MyCurrentPanel() {
       selectedObjects = new CompoundPropertyModel<MyObject>(new MyObject());
       group = createCheckGroup();
       group.setOutputMarkupId(true);
       add(group);
   }

   public CheckGroup<MyObject> createCheckGroup() {
   CheckGroup<MyObject> newGroup = new CheckGroup<MyObject>(ID, MyCurrentPanel.this.selectedObjects);
    ListView<MyObject> objectList = new ListView<MyObject>(ID, values) {
        @Override
            protected void populateItem(ListItem<MyObject> item) {
                Check<MyObject> check = new Check<MyObject>(TIME_CHECK, item.getModel());
                Label l = new Label(TIME_LABEL, item.getModel());
                item.add(check);
                item.add(l);
            }
    }
    newGroup.add(objectList);
    newGroup.add(new AjaxFormChoiceComponentUpdatingBehavior() {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                System.out.println("Selected objects: "+selectedObjects.size());
                CheckGroup<MyObject> updateGroup = createCheckGroup();
                updateGroup.setOutpurMarkupId(true);
                MyCurrentPanel.this.group.replaceWith(updateGroup);
                MyCurrentPanel.this.group = updateGroup;
                target.add(MyCurrentPanel.this.group);
            }
        });
    }