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

自作多情 提交于 2019-12-10 13:46:37

问题


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.


回答1:


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});
});



回答2:


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;
}



回答3:


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});



回答4:


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() ?>



回答5:


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});


来源:https://stackoverflow.com/questions/39563401/how-to-refresh-pjax-listview-in-yii2-it-reloads-the-entire-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!