pjax reload with async set to false

Suddenly, pjax reload fails to reload the GridView. It instead refreshes the page and I have no idea why. Is it new thing with the recent Yii2 release?

Here is the code


$.pjax.reload({container:'#my-grid-view'});

To fix this issue, I set async to false (from stackoverflow) as follow:


$.pjax.reload({container:'#my-grid-view', async: false});

I tried to search for pjax async option and found nothing about async. Any idea what pjax async flag does?

Hi,

When you click the pagination link? or while searching the filters the reload occurs?

Make sure your controller returns if the ajax request then renderPartial used.

Pagination works fine. I have a modal that after user enter new item, the item grid should update using the js


$.pjax.reload({container:'#my-grid-view'});

You most likely have multiple pjax or pjax widget inside of another pjax. There really isn’t a fix just a bunch of work arounds. The async option is only used to update multiple at once.

I went and dug out this little gem as my workaround for a lot of these issues. You can just call pjaxReload(); and it will reload the most parent pjax if they are nested or it will reload all of them on the page. You can also tell it to reload specific ids by doing pjaxReload(‘my-grid-view’); or comma delimit them to add a few of them. Unfortunately it was minified, I spaced it properly but it still looks crazy.





pjaxReload = function(a) {

    if (!a) var a = jQuery("[data-pjax-container]").map(function() {

        var a = jQuery(this).attr("id");

        if (1 !== jQuery("#" + a).parents("[data-pjax-container]").length) return a

    }).get().join(",");

    if (void 0 !== typeof a) {

        var e = a.split(",");

        1 === e.length ? jQuery.pjax.reload({

            container: "#" + jQuery.trim(a)

        }) : e.length > 1 && jQuery.each(e, function(a) {

            "" !== e[a] && jQuery.pjax.reload({

                container: "#" + jQuery.trim(e[a]),

                async: !1

            })

        })

    }

};

As skworden said, you’re probably nesting pjax. Something that worked for me is the following:




var url = 'index.php?r=controller/action';

$.pjax.reload({url: url, container: '#containerToReload', push: false, replace: false, timeout: 8000});



This will reload the url that you pass. Note this will reload the whole page, not just the gridview, it has pushState disabled as well.

In cases you need to use nested pjax, I would recommend switching to regular ajax + push state if you need it.

1 Like

thank you