简体   繁体   中英

Wicket: Get notified when no checkbox is selected anymore

This is part of a Wicket GUI (Wicket 7.6.0) which is acting as a front end to display some database entries.

On this specific page we have a form where a user can search for entries by specifiyng eg. a start date, end date etc. to limit the results. After hitting the button the search entries are displayed below this search form.

These results are displayed using a Wicket DataTable with pagination. The entries in that table have a check box. This is implemented by a custom CheckBoxPanel (extended from Panel ) to which a Check instance ( org.apache.wicket.markup.html.form.Check ) is added.

Now, I want to react to each select and unselect of a checkbox. The reason for this is that we want to keep track of the items of the selection even on pagination in a second list.

I added the AjaxFormChoiceComponentUpdatingBehavior to the group (see source of method addCheckboxes below). This works as long as at least one checkbox is selected - for every click on a checkbox the debug statement is printed.

But when the last checkbox is unselected the onUpdate() method is not called, despite the fact that an AJAX call is made (verified by using Firefox Developer Tools ). The request payload is group%3Aactions= . When I select an item the request payload is eg: group=check4&group%3Aactions= .

My question is: Is there a way to get notified if none items are selected anymore? This is needed to keep the before mentioned "second list" in sync.

A solution does not need to use AJAX, a classic POST solution would also be ok.

private void addCheckboxes() {
    System.out.println("addCheckboxes called!");
    selectedModel = new CollectionModel<FaultModel>(new ArrayList<FaultModel>());
    group = new CheckGroup<FaultModel>("group", selectedModel);
    group.onSelectionChanged();
    group.setRequired(Boolean.TRUE);

    CustomCheckGroupSelector<FaultModel> groupSelector = new CustomCheckGroupSelector<>("groupselector", rows.isEmpty());

    RemarkModalWindow remarkModal = new RemarkModalWindow("remarkModal", selectedModel);
    IndicatingAjaxButton actionButton = new ActionButton<FaultModel>("execute", this, remarkModal, rows.isEmpty());

    DefaultDataTable<?, String> table = TableFactory.getTable2(CustomType.FAULT, rows, page);
    group.add(table);
    group.add(groupSelector);
    group.add(remarkModal);
    group.add(actionButton);

    group.add(new AjaxFormChoiceComponentUpdatingBehavior() {
        @Override protected void onUpdate(AjaxRequestTarget target) {
            System.out.println("!! onUpdate !! Adding selected items to second list.");
        }
    });

    add(group);
}
```

The problem is, that you have set group.setRequired(Boolean.TRUE); so not having any selection at all actually is not a valid state which is why the onUpdate(AjaxRequestTarget target) of your AjaxFormChoiceComponentUpdatingBehavior is not called.

You could either set group.setRequired(false); or override the onError(AjaxRequestTarget target, RuntimeException e) method in your AjaxFormChoiceComponentUpdatingBehavior, which gets called when (among other errors) no elements are selected while the form component is required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM