📜 ⬆️ ⬇️

Using Pjax in Yii2 (short review)

I am developing a project on yii2, at the same time 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 take time to 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 a page not completely when following links, but partially, but with the appropriate page title and the ability to go back

Using Pjax with a GridView


Well written about it here .

If in brief, in order for pjax to work, you need to wrap the GridView widget as follows:
')
<?php \yii\widgets\Pjax::begin(); ?> <?= GridView::widget([ // ... configuration here ]); <?php \yii\widgets\Pjax::end(); ?> 


If you do not want all links to perform a pjax request, then the data-pjax = 0 attribute should be added to the excluded links. For example:

 <?= \yii\helpers\Html::a(Yii::t('app', '...'), ['car/view', 'id' => $car->id], ['data-pjax'=>0]) ?> 


Also, the pjax widget has a property:

 public $linkSelector; 


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

pjax to submit forms

If you want to hang pjax on a form submit, 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

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

Source: https://habr.com/ru/post/225823/


All Articles