Using Pjax in Yii2 (Short Review)

I am developing a project on yii2, while studying the framework, and could not help but share one of the wonderful tools that it represents. The post will be useful to those who have not worked with Pjax before. Experienced developers who will devote time to the post will be grateful for pointing out inaccuracies and additions, so the post will become more useful and informative.

For reference:
Pjax is a jquery plugin that uses pushState and ajax and provides the ability to load the page not completely when following links, but partially, but with the corresponding page title and the ability to go back

Using Pjax with a GridView


Well written about it here .

In short, in order for pjax to work, you need to wrap the GridView widget as follows:



If you do not want all links to make a pjax request, add the data-pjax = 0 attribute to the excluded links . For example, like this:

 $car->id], ['data-pjax'=>0]) ?>


The pjax widget also has a property:

public $linkSelector;


It can be used to fetch links that will be processed using pjax.

pjax on submit form

If you want to hang pjax on the form submission, then you should add the data-pjax = 1 attribute to it, since by default the code generated by the widget looks like this:

jQuery(document).on('submit', "#w2 form[data-pjax]", function (event) {jQuery.pjax.submit(event, '#w2', {"push":true,"replace":false,"timeout":1000,"scrollTo":false});});


However, you can also change it using the property:

public $formSelector;


Where to read more

Link to pjax widget github.com/yiisoft/yii2-framework/blob/master/widgets/Pjax.php
Link to pjax plugin github.com/yiisoft/jquery-pjax

Also popular now: