How to update pjax list in yii2? It reloads the entire page.

I want to update pjax list without refreshing the whole page. This is a representation of only the pjax list itself.

<?= Html::Button('refresh-countries', ['class' => 'btn btn-primary', 'name' => 'login-button', 'id'=>'refresh']) ?>  

<?php Pjax::begin(['id' => 'countries']) ?>

    <?= ListView::widget([
         'dataProvider' => $dataProvider,
         'itemOptions'  => ['class' => 'comment-item'],
         'itemView'     => 'commentadapter',

    ]); ?> 

<?php Pjax::end() ?>

Please, I want it to update onclick of this button, and only the list will be updated. I know how to do this, but it refreshes the whole page.

+6
source share
5 answers

You should like this:

 use yii\widgets\Pjax;

<?php Pjax::begin(['id' => 'some_pjax_id']) ?>
     //your code 
 <?php Pjax::end() ?>

the above form is contained in the tab, and here is my script to reload pjax:

$("#my_tab_id").click(function() {
    $.pjax.reload({container: '#some_pjax_id', async: false});
});
+7
source

PJAX has an option timeout. If PJAX does not receive an AJAX response during this timeout, it will complete a page reload. Use the following JS snippet:

$.pjax.defaults.timeout = false;       // For JS use case yor should manual override default timeout.
$.pjax.reload({container: '#pjaxId'});

or shorter fragment:

$.pjax.reload('#pjaxId', {timeout : false});

, Pjax:

/**
 * Custom Pjax with incremented timeout.
 * JS for Pjax updating:
 *  <code>
 *      $.pjax.defaults.timeout = false;             // For JS use case yor should manual override default timeout.
 *      $.pjax.reload({container: '#pjaxId'});
 *
 *      // OR
 *      $.pjax.reload('#pjaxId', {timeout : false});
 *
 *      // OR for gridview with search filters
 *      $('.grid-view').yiiGridView('applyFilter'); // Thats true only if you have search Filters
 *  </code>
 *
 * Note: In more cases ID of widget should be static, because widgetId is autoincremented and browser version of page may be not up-to-date.
 */
class Pjax extends \yii\widgets\Pjax
{
    /**
     * @var int Timeout {@link \yii\widgets\Pjax::$timeout}.
     *          For JS use case yor should manual override defaults (  $.pjax.defaults.timeout = false;  ).
     */
    public $timeout = 30000;
}
+4

, Refesh Pjax: begin(). url url.

<?php Pjax::begin(['id' => 'countries']) ?>
    <?= Html::a("Refresh Countries", ['Your Current Url to view Country'], ['class' => 'btn btn-lg btn-primary']);?>
             <?=  ListView::widget([
                 'dataProvider' => $dataProvider,
                 'itemOptions' => ['class' => 'comment-item'],
                 'itemView' => 'commentadapter',

            ]);
                ?>
    <?php Pjax::end() ?>
0

/ GjView Yii2 Pjax

<?php 
use yii\widgets\Pjax;
?>
<?php Pjax::begin(['id' => 'list-data-list', 'timeout' => false, 'enablePushState' => false]); ?>  
<?=
GridView::widget([
dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "<div class='tab-bg'>{summary}</div>\n\n<div class='table table-responsive list-table'>{items}\n{pager}</div>",
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'data_id',
[
'attribute' => 'data_type',
'label' => 'Data Type',
'format' => 'raw',
'value' => function ($model) {
    return ($model->data_subtype_id) ? $model->dataName->parentDataName->name : '-';
}, 
],
]
]);?>
 <?php Pjax::end(); ?>

Here is a script to upload updated data to the grid list.

$.pjax.reload({container: "#list-data-list", async: false});
0
source

Just reload the grid using the current filter:

$(".grid-view").yiiGridView("applyFilter");

Reset Advanced Search Filter:

$("#search-form").find("input, select").val(""); // clear search form fields
$(".grid-view .filters input").val(""); // clear grid filters
window.history.pushState('object', document.title, window.location.href.split('?')[0]);
$.pjax.reload({container: '#wrapper-pjax', async: false});
0
source

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


All Articles