diminuta
diminuta

Reputation: 1583

Hide/Show a Panel depending on a CheckBox in Wicket

I have this piece of code:

final WebMarkupContainer container = new WebMarkupContainer("container");
container.setVisible(aBooleanValue);
container.setOutputMarkupId(true);
container.setOutputMarkupPlaceholderTag(true);
add(container);

final CheckBox hideContainer = new CheckBox("hideContainer", new PropertyModel<Boolean>(getModel(), "hideContainer"));
container.add(hideContainer);
hideContainer.add(new AjaxFormComponentUpdatingBehavior("onchange") {
    
    private static final long serialVersionUID = 1L;

    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        if (Boolean.TRUE.equals(getModelObject().getHideContainer())) {
            container.setVisible(false);
        } else {
            container.setVisible(true);
        }
        target.addComponent(MyForm.this.get("container"));
        
    }
});

Which creates a WebMarkupContainer and a Checkbox, if the checkbox is checked then the container should be hidden, if the checkbox is not checked, then the container should be visible.

But this doesn't work... what am I doing wrong?

Upvotes: 0

Views: 1480

Answers (2)

pundit
pundit

Reputation: 312

A best wicket practice is to configure wicket components to pull data rather than pushing data to them. Instead of setting the visibility of the component can you please try to override the isVisible method of the component.

final WebMarkupContainer container = new WebMarkupContainer("container"){

   public boolean isVisible(){
      return showContainer;//showContainer is a boolean instance variable
   }            

};

And in the onchange event change the showContainer value and add the component to target.

Upvotes: 1

Michał Krzemiński
Michał Krzemiński

Reputation: 1251

You could try using AjaxCheckBox. Override onUpdate(AjaxRequestTarget) method and move code from behavior, as it should work correctly on all browsers.

Upvotes: 2

Related Questions