How to refresh pjax listview in yii2? It reloads the entire page
Asked Answered
H

5

8

I want to be able to refresh a pjax listview without refreshing the whole page. This is the view just 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 refresh onclick of that button and only the listview would refresh. I know how to do it but it refreshes the whole page.

Hembree answered 18/9, 2016 at 22:56 Comment(0)
P
12

You have to like this:

 use yii\widgets\Pjax;

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

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

$("#my_tab_id").click(function() {
    $.pjax.reload({container: '#some_pjax_id', async: false});
});
Paperweight answered 19/9, 2016 at 7:11 Comment(2)
thank you but it did not work did not work .it does not reload or updateHembree
i used it with jqueryHembree
M
5

PJAX has timeout option. If PJAX not obtain AJAX response during this timeout, it will perform full page reload. Use following JS snippet:

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

or more short snippet:

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

Moreover in my projects I use overrided version of 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;
}
Mintun answered 20/9, 2016 at 16:58 Comment(0)
R
2

Just to reload grid applying current filter:

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

Reseting 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});
Rossiya answered 23/8, 2019 at 22:48 Comment(0)
J
0

Try but Refesh button below Pjax:begin(). and set url same Current url.

Example

<?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() ?>
Joule answered 19/9, 2016 at 3:34 Comment(2)
it refreshes i dont want the page to refreshHembree
It refreshes listview with pjax. The given Example is correct. There is no other way to refresh without doing a request to some action.Tranquillity
A
0

Yii2 GridView Listing Refresh/Update Via 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 Script to Load updated data in to gridview list.

$.pjax.reload({container: "#list-data-list", async: false});
Artair answered 7/3, 2019 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.