如何刷新yii2中的pjax listview?它重新加载整个页面

问题描述 投票:5回答:4

我希望能够在不刷新整个页面的情况下刷新pjax listview。这只是pjax列表本身的视图。

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

我希望它刷新该按钮,只有列表视图会刷新。我知道怎么做,但它刷新了整个页面。

php yii2 pjax
4个回答
6
投票

你必须这样:

 use yii\widgets\Pjax;

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

选项卡中包含上面的表单,这是我重新加载pjax的脚本:

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

4
投票

PJAX有timeout选项。如果PJAX在此超时期间未获得AJAX响应,则它将执行整页重新加载。使用以下JS代码段:

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

或更简短的片段:

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

0
投票

尝试在Pjax下方刷新按钮:begin()。并设置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
投票

Yii2 GridView列表通过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(); ?>

这是将更新数据加载到gridview列表的脚本。

$.pjax.reload({container: "#list-data-list", async: false});
© www.soinside.com 2019 - 2024. All rights reserved.