fred
fred

Reputation: 1822

Wicket label + Ajax not working

I have a simple, mysterious problem with a label and using ajax to show it.

public class ChecklistTemplateForm extends Form{
    private static final long serialVersionUID = 1L;
    private Label cantSaveLabel;
    public ChecklistTemplateForm(String id) {
        super(id);
        cantSaveLabel = new Label("cantSaveLabel", "Name is not unique, enter another name and try saving again.");
        cantSaveLabel.setVisible(false);
        cantSaveLabel.setOutputMarkupId(true);
        add(cantSaveLabel);
        add(new AjaxButton("saveButton") {
            private static final long serialVersionUID = 1L;
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(cantSaveLabel);
                //here i do some stuff to decide if canSave is true or false
                if (canSave){
                    setResponsePage(AdminCheckListPage.class);
                }
                else if (!canSave){
                    cantSaveLabel.setVisible(true);
                    System.out.println(canSave);
                }
            }
        }); 
    }

}

The funny thing is, is canSave is false, the System.out.print works but the cansavelabel never becomes visible. What am I missing?

Upvotes: 5

Views: 3643

Answers (2)

Till
Till

Reputation: 994

You have to tell wicket that it should use a placeholder for you label because it can't update a component that doesn't exist in the markup.

cantSaveLabel.setOutputMarkupId(true);
cantSaveLabel.setOutputMarkupPlaceholderTag(true);

Upvotes: 13

bert
bert

Reputation: 7696

You can not update the Label via Ajax as it is not in the rendered page. The

cantSaveLabel.setVisible(false); 

makes it so that the Label is not in the HTML. You would need to surround the Label with another component (WebMarkupContainer), call setOutputMarkupId(true) on this and add this container to the AjaxRequestTarget instaed of the label.

Upvotes: 2

Related Questions