【问题标题】:How to refresh pjax listview in yii2? It reloads the entire page如何在 yii2 中刷新 pjax listview?它重新加载整个页面
【发布时间】:2019-08-24 06:48:03
【问题描述】:

我希望能够在不刷新整个页面的情况下刷新 pjax 列表视图。这是 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


    【解决方案1】:

    你必须喜欢这样:

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

    【讨论】:

    • 谢谢你,但它没有工作没有工作。它不会重新加载或更新
    • 我用它和 jquery 一起使用
    【解决方案2】:

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

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

    或更短的sn-p:

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

    【讨论】:

      【解决方案3】:

      只是为了重新加载网格应用当前过滤器:

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

      重置高级搜索过滤器:

      $("#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】:

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

        【讨论】:

        • 它刷新我不希望页面刷新
        • 它使用 pjax 刷新列表视图。给定的示例是正确的。如果不请求某些操作,则没有其他方法可以刷新。
        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-04
          • 2017-02-04
          • 2013-02-22
          相关资源
          最近更新 更多