yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

GridView with PJAX don't work AJAX delete #8301

Closed maximpn closed 8 years ago

maximpn commented 9 years ago

I have seen some many posts about this problem, but all of they don't introduce clarity. I am use solution from box. GridView + Pjax and delete action. When I will try to delete item the Pjax reload all page, when I do redirect in my delete acton. What I do wrong?

Yii 1.1 send ajax request and then update GridView by update js method. I don't see similar behavior in Yii 2.

cebe commented 9 years ago

So you expect Pjax to follow the redirect of the delete action (assuming it redirects to the index page that contains the gridview) and reload the grid only instead of following the redirect for the whole page?

maximpn commented 9 years ago

I think this is simplest and straight way. But the PJAX headlines will be lost after the browser will be redirected from delete action to the index action. The index action thinking that redirect request is usually not PJAX (or AJAX) request and return full web page. Sure, PJAX will reload full web page.

And other question: what about filter and page params? Because they will be lost after redirect.

P.S. I think true way use AJAX request to delete action and then refresh current page by pjax update.

What is the philosophy of Yii2 about buttons with AJAX action in GridView?

maximpn commented 9 years ago

I am using this enhancement of yii.handleAction method in yii.js:

if (isAjax) {
        $.ajax({
            url: $form.attr('action'),
            method: $form.attr('method'),
            data: $form.serialize(),
            success: function (data) {
                if (data) {
                    var $pjaxContainer = $e.closest('.pjax');
                    if ($pjaxContainer.length && $.pjax.reload) {
                        $.pjax.reload('#' + $pjaxContainer.attr('id'), {
                            url: window.location.href
                        });
                    } else {
                        window.location.reload();
                    }
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('Error was thrown while AJAX request: ' + textStatus);
            }
        });
    } else {
        $form.trigger('submit');
    }

It's requires that pjax container has .pjax class and button or link has data-ajax="1" attribute.

yurii-github commented 9 years ago

this? use yii\widgets\Pjax; Pjax::begin(); echo GridView::widget([....] Pjax::end();?>