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

Pjax and Pagination #2495

Closed alex-code closed 10 years ago

alex-code commented 10 years ago

Not sure if it's a real issue or not, or if it should be dealt with by Pjax js.

When using Pjax with Pagination it will append the _pjax query string parameter to the paging a elements. The Pjax js then also appends the query string again so the request made is ?page=2&_pjax=#w0&_pjax=#w0

VinceG commented 10 years ago

@Alex-Code Could you share some code. i can't confirm this on my end.

I've used something as simple as this:

Pjax::begin();
echo GridView::widget(['dataProvider' => $provider]);
Pjax::end();

and i got: http://dev.com/yii2/apps/basic/web/index.php?r=site/about&page=6&_pjax=%23w0

alex-code commented 10 years ago

This will only occur on the second request. The first request will send the _pjax parameter which then gets put in the paging query string by Yii.

So the href on the paging links are like ?page=1&_pjax=#w0 When a paging link is then clicked again PJax will then also append the _pjax parameter again.

It can be fixed by putting this in the init() of PJax. \yii\helpers\ArrayHelper::remove($_GET, '_pjax');

zelenin commented 10 years ago

I confirm it. "_pjax" appends to sorting links too.

tonydspaniard commented 10 years ago

@qiangxue confirmed... Nevertheless, this is not really a bug as pjax issue is just appending twice the same parameter. The issue seems not to affect its functionality.

The only way to tackle this issue is by working on the library pjaxit self.

tof06 commented 10 years ago

The only way to tackle this issue is by working on the library pjax it self.

Well, not sure this is a pjax issue. As far as I understand the pjax library, pjax add this parameter for caching issues :

    // We want the browser to maintain two separate internal caches: one
    // for pjax'd partial page loads and one for normal page loads.
    // Without adding this secret parameter, some browsers will often
    // confuse the two.
    if (!options.data) options.data = {}
    options.data._pjax = context.selector

But, when creating links (for pager or sorters), Yii will add all current GET parameters (to keep track of current filters), and therefore, will add this _pjax to all links.

We can easily fix that by removing the _pjax parameter when rendering view inside the pjax container.

EDIT: Sorry, didn't see that the solution was already proposed by @Alex-Code

tof06 commented 10 years ago

I've checked again on how we can fix it in jquery-pjax.js. In fact, it can be easily solved by stripping the _pjax parameter from the URL just before calling $.ajax(). I'll make a new PR on the new yiisoft/jquery-pjax external package.

tof06 commented 10 years ago

I think this issue can be closed now.

babbyhh commented 5 years ago

I have error with _Pjax but I not think how solution, and I read this is issues and I think isnt good change vendor. In my gridview I have one Url so after on click the url change for end "&_Pjax=..."
and I have error:

Uncaught TypeError: Cannot read property '0' of null at extractContainer (:733:69) at jQuery.fn.init.options.success (:301:21) at fire (jquery.js:3291) at Object.fireWith [as resolveWith] (jquery.js:3421) at done (jquery.js:9533) at XMLHttpRequest. (jquery.js:9785)"