Reputation: 65
I am using wicket's CheckBoxMultipleChoice to let the user set a list of options. so far it works fine. but then i want to add a "check all" check box that checks all the options in the CheckBoxMultipleChoice and I am having problems with that.
here is my initial code
ArrayList<String> chosen;
List<String> choices = Arrays.asList(new String[]{"Train", "Bus", "Car"});
CheckBoxMultipleChoice myCheck = new CheckBoxMultipleChoice("transport", new Model(chosen), choices));
myCheck.setOutputMarkupId(true);
form.add(myCheck);
on submit i print out the values of chosen and its "Bus", "Car" etc. as expected.
now i am adding a checkbox to check all the choices using ajax:
Boolean checkOrNot;
final CheckBox checkAll = new CheckBox("checkAll", new Model(checkOrNot));
form.add(checkAll);
checkAll.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
// here i am not able to set the checkboxes
// i tried doing this
chosen.clear();
chosen.add(new String("Car"));
chosen.add(new String("Train"));
myCheck.modelChanged();
// i have also tried recreating the multiple choice
myCheck = new CheckBoxMultipleChoice<T>("transport", new Model(chosen), choices);
myCheck.setOutputMarkupId(true);
target.addComponent(myCgecj);
target.addComponent(form);
}
});
I am running out of ideas and wondering if anyone has any solutions? thanks in advance for any help.
Upvotes: 4
Views: 9202
Reputation: 2244
I know that you fount already an answer but in my case I had exactly the same issue, but the option to use the CheckGroup was not feasible because my list is built dynamically and at the beginning it can even be empty.
The situation for me was:
Thanks god I found This Forum in Nabble.
Basically here Pedro Santos propose to use the AjaxFormChoiceComponentUpdatingBehavior instead. I followed the advice and finally I was able to get the values in the model.
Sometimes I think Wicket is Wicked :P
Upvotes: 0
Reputation: 7272
Well, another way to go is to use
checkBoxMultipleChoice.setDefaultModelObject(listOfAllElements); // Select all.
checkBoxMultipleChoice.setDefaultModelObject(Lists.newArrayList()); // Deselect all.
Upvotes: 1
Reputation: 10896
You could use javascript to mark the checkboxes.
An example, using jQuery:
mycheck.setOutputMarkupId(true);
checkAll.add(new SimpleAttributeModifier("onclick",
"$('#" + mycheck.getMarkupId() + " input:checkbox').attr('checked', $(this).is(':checked'))");
Upvotes: 1
Reputation: 5575
I didn't (and can't) try that right now, so this is to be taken with a grain of salt, but couldn't you use a CheckGroup for that?
From the JavaDoc:
Component used to connect instances of Check components into a group. Instances of Check have to be in the component hierarchy somewhere below the group component. The model of the CheckGroup component has to be an instance of java.util.Collection. The model collection of the group is filled with model objects of all selected Check components.
So a
new Checkgroup("group", choices)
should work for you. No need to reimplement that functionality.
P.S.: I'll check that as soon as I've got the chance to do so...
Upvotes: 3