How to open ModalDialog on PageLoad
Asked Answered
W

2

10

How can I open a modal dialog on PageLoad(), in the constructor of the WebPage and without the AjaxRequestTarget, with Wicket?

Wolsey answered 22/1, 2011 at 2:28 Comment(1)
In wicket 6 you can add an AjaxEventBehavior to your page and open your modal inside the behavior. See https://mcmap.net/q/1165216/-wicket-6-calling-javascript-function-after-page-loadCouch
V
11

I couldn't find a way to open it without an Ajax request, but it's perfectly possible to open it when the page is loaded, with a simple behavior:

HomePage.java

public class HomePage extends WebPage {
    public HomePage(PageParameters pageParameters) {
        super(pageParameters);

        ModalWindow modal = new ModalWindow("modal");
        modal.add(new OpenWindowOnLoadBehavior());
        modal.setPageCreator(new ModalWindow.PageCreator() {
            @Override
            public Page createPage() {
                return new RedirectPage("http://www.google.com");
            }
        });
        add(modal);
    }
}

OpenWindowOnLoadBehavior.java

public class OpenWindowOnLoadBehavior extends AbstractDefaultAjaxBehavior {
    @Override
    protected void respond(AjaxRequestTarget target) {
        ModalWindow window = (ModalWindow) getComponent();
        window.show(target);
    }
    @Override
    public void renderHead(IHeaderResponse response) {
        response.renderOnLoadJavascript(getCallbackScript().toString());
    }
}

HomePage.html

<html xmlns:wicket="http://wicket.apache.org">
<body>
  <div wicket:id="modal"></div>
</body>
</html>
Vervain answered 22/1, 2011 at 11:47 Comment(0)
L
1

Opening a modal window on page load (no AJAX involved) described in Wicket Wiki

 /**
 * @author Ernesto Reinaldo Barreiro 
 */
 public class OpenOnLoadModalWindow extends ModalWindow implements IHeaderContributor {

       private static final long serialVersionUID = 1L;

       /**
        * @param id
        */
       public OpenOnLoadModalWindow(String id) {
               super(id);
       }

       /**
        * @param id
        * @param model
        */
       public OpenOnLoadModalWindow(String id, IModel<?> model) {
               super(id, model);
       }

       /**
        * Adds the JavaScript to initially open modal window.
        */
       public void renderHead(IHeaderResponse response)
       {
               response.renderOnDomReadyJavascript(getWindowOpenJavascript());
       }

       /*
        * (non-Javadoc)
        * @see org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow#makeContentVisible()
        */
       @Override
       protected boolean makeContentVisible()
       {
               return true;
       }
}
Legal answered 26/11, 2013 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.