Zend Framework 2 & jquery modal dialog

2020-06-29 12:50发布

How does one go about displaying a controller action inside of jquery modal dialog?

2条回答
聊天终结者
2楼-- · 2020-06-29 13:24

Firstly you'll need your Javascript to load a url via ajax, this will depend on which kind of modal you are using etc, there's a ton of libraries out there. I will assume you are using the basic JQuery UI dialog Modal.

Example Link

<!-- this points to your action below.. -->
<a class="some-link" title="title here" href="mycontroller/test">testing</a>

Example Javascript (quick example found on google, many examples out there..)

$(document).ready(function() {
    $('.some-link').each(function() {
        var $link = $(this);
        var $dialog = $('<div></div>')
            .load($link.attr('href'))
            .dialog({
                autoOpen: false,
                title: $link.attr('title'),
            });
    });
});

Now you need to make sure your action doesn't render the main layout when providing the content for the modal via the ajax request.

Here's a really simple method of doing that by replacing the base layout with an empty view for ajax requests. This isn't the best method but it's the simplest for this case ;)

Example Action

public function testAction()
{
    if($this->getRequest()->isXmlHttpRequest()) {
        $this->layout('application/layout/ajax-layout');
    }

    return new ViewModel(array()); // ..
}

application/layout/ajax-layout.phtml

<?php echo $this->content ?>
查看更多
3楼-- · 2020-06-29 13:44

I think you want this kind of code http://jqueryui.com/dialog/#modal-message inside the just display your action

Otherwise it's about to open an url into your modal it's like that http://blog.nemikor.com/2009/04/18/loading-a-page-into-a-dialog/

查看更多
登录 后发表回答