mardi 15 septembre 2015

How to synchronize an Ajax onClick and onUpdate Event

I have a table to display the rights of the selected user. In the columns I have the role and in the rows the function. In each cell within the table I have a Wicket AjaxCheckBox, which uses an onUpdate Event to trigger an action. But in addition to that the admin should also be able to click on the row to select the Checkbox. Each cell has therefore an onClick event attached.

If I click the CheckBox, it won't select, but if I double-click it does. Since I trigger with one click the onClick of the cell and the onUpdate of the CheckBox simulatanisly it neutralizes itself.

Right now I'm runnig it like this:

final WebMarkupContainer cell = new WebMarkupContainer("cont");
cell.setOutputMarkupId(true);
cell.setOutputMarkupPlaceholderTag(true);
cell.setVisibilityAllowed(true);
cell.add(AttributeModifier.append("class", checkState));

cell.add(new AjaxEventBehavior("onclick") {
    private static final long serialVersionUID = 1L;
        @Override
        protected void onEvent(AjaxRequestTarget target) {
            eventHandler(checked, checkState);
            target.add(cell);
        }
});

item.add(cell);
cell.add(new AjaxCheckBox("checkbox", checked) {
    private static final long serialVersionUID = 1L;
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        eventHandler(checked, checkState);
        target.add(cell);
}});

public void eventHandler(IModel<Boolean> checked, IModel<String> checkState) {
    boolean isChecked = checked.getObject();
    if (isChecked == false) {
        checked.setObject(true);
        checkState.setObject("checked");
        //Do sth
    } else if (isChecked == true) {
        checked.setObject(false);
        checkState.setObject("unchecked");
        //Do sth
    }
}




Aucun commentaire:

Enregistrer un commentaire