Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 719
fred Avatar asked Dec 09 '11 08:12

fred


2 Answers

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);
like image 53
Till Avatar answered Nov 20 '22 01:11

Till


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.

like image 34
bert Avatar answered Nov 19 '22 23:11

bert