Wicket ModalWindow:单击模式外部以关闭

发布于 2024-11-15 00:03:29 字数 464 浏览 1 评论 0原文

我需要向 ModalWindow 添加一个行为,以便当用户在模式之外单击时,如果满足特定条件(在我的 respond() 方法中检查服务器端),模态被驳回。

我尝试根据这个答案实现一些东西。问题是我无法将点击侦听器附加到模式窗口 div,因为在显示模式之前它不会添加到 DOM(该示例将 keyup 附加到文档本身。)

我想扩展 ModalWindow 并通过 getShowJavascript() 添加 JS,但是我没有任何方法将其连接到用于测试的 AbstractDefaultAjaxBehavior.respond() 方法。

I need to add a behavior to ModalWindow such that when a user clicks outside of the modal, if a certain condition is met (checked server-side in my respond() method), the modal is dismissed.

I tried implementing something based on this answer. The problem is that I can't attach a click listener to the modal window div because it's not added to the DOM until the modal is shown (the example attaches a keyup to the document itself.)

I thought of extending ModalWindow and adding the JS via getShowJavascript(), but then I don't have any means to connect it to an AbstractDefaultAjaxBehavior.respond() method for the test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2024-11-22 00:03:29

我通过将行为添加到通过 ModalWindow.setContent() 添加到模式的小部件中,而不是添加到模式中来解决这个问题。当我的小部件添加到模式中时,掩码 div 出现在 DOM 中,然后我可以成功附加单击处理程序。

也就是说:

public static class CloseModalOnOutsideClickBehavior extends AbstractDefaultAjaxBehavior
{
    private final ModalWindow modal;

    public CloseModalOnOutsideClickBehavior(ModalWindow modal)
    {
        this.modal = modal;
    }

    @Override
    protected void respond(AjaxRequestTarget target)
    {
        if (myConditionIsMet()) modal.close(target);
    }

    @Override
    public void renderHead(IHeaderResponse response)
    {
        response.renderJavascriptReference("http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js");
        response.renderJavascript("" +
                "$(document).ready(function() {\n" +
                "  $('div.wicket-mask-dark').bind('click', function(evt) {\n" +
                        getCallbackScript() + "\n" +
                "        evt.preventDefault();\n" +
                "  });\n" +
                "});", "closeModal");
    }
}

请注意,我有点不稳定地依赖于模态窗口的掩码,该掩码具有名为“wicket-mask-dark”的类;如果您更改 ModalWindow 使用的 CSS 设置,情况可能并非如此。

I solved this by adding the behavior not to the modal, but rather to the widget I add to the modal via ModalWindow.setContent(). When my widget is added to the modal, the mask div is present in the DOM, and I can then successfully attach a click handler.

To wit:

public static class CloseModalOnOutsideClickBehavior extends AbstractDefaultAjaxBehavior
{
    private final ModalWindow modal;

    public CloseModalOnOutsideClickBehavior(ModalWindow modal)
    {
        this.modal = modal;
    }

    @Override
    protected void respond(AjaxRequestTarget target)
    {
        if (myConditionIsMet()) modal.close(target);
    }

    @Override
    public void renderHead(IHeaderResponse response)
    {
        response.renderJavascriptReference("http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js");
        response.renderJavascript("" +
                "$(document).ready(function() {\n" +
                "  $('div.wicket-mask-dark').bind('click', function(evt) {\n" +
                        getCallbackScript() + "\n" +
                "        evt.preventDefault();\n" +
                "  });\n" +
                "});", "closeModal");
    }
}

Note that I'm somewhat precariously depending on the modal window's mask having a class named "wicket-mask-dark"; this may not be the case if you alter the CSS settings used by ModalWindow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文