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

ari*_*nze 6 php pjax yii2

我希望能够刷新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() ?>
Run Code Online (Sandbox Code Playgroud)

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

Rah*_*war 9

您必须这样:

 use yii\widgets\Pjax;

<?php Pjax::begin(['id' => 'some_pjax_id']) ?>
     //your code 
 <?php Pjax::end() ?>
Run Code Online (Sandbox Code Playgroud)

标签中包含的上述表格,这是我的脚本来重新加载pjax:

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


ISt*_*ger 5

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

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

或更短的片段:

$.pjax.reload('#pjaxId', {timeout : false});
Run Code Online (Sandbox Code Playgroud)

此外,在我的项目中,我使用 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;
}
Run Code Online (Sandbox Code Playgroud)


dun*_*uan 0

尝试 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() ?>
Run Code Online (Sandbox Code Playgroud)