0

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
Cajova_Houba
  • 44
  • 1
  • 9
  • you are adding new AjaxFormChoiceComponentUpdatingBehavior() to your group, but if it exists it will throw an error. You can try group.addOrReplace(...) or else add the AjaxForm on a repeating view (i.e. objectlist, and use a child id ore something) – Igoranze May 10 '16 at 09:46
  • Unfortunately, addOrReplace() method can only be used for Components, not for Behaviors. And since the model to be updated (selectedObjects) is in the CheckGroup, the updating behavior should also be added to the checkgroup, shouldn't it? At least this is how I understand this mechanism in wicket. – Cajova_Houba May 10 '16 at 10:14
  • Do you have a stacktrace? – Igoranze May 10 '16 at 12:34
  • I'm not getting any exception, I will add an example of my problem in the main post. – Cajova_Houba May 10 '16 at 16:20
  • Please create a quickstart application demonstrating the problem. E.g. at GitHub/BitBucket. – martin-g May 11 '16 at 19:59

3 Answers3

1

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

0

Try

target.add(form);`

or

target.add(group);

or something similar, depending on your code.

0

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);
            }
        });
    }
GeppyZ
  • 111
  • 4