本文介绍了当超链接不在 pjax 中时,yii2 如何使用 pjax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在yii2中使用pjax,就像:

To use pjax in yii2, just like:

<?php Pjax::begin(); ?>
<?= Html::a("Refresh", ['site/index'], ['class' => 'btn btn-lg btn-primary']);?>
<h1>Current time: <?= $time ?></h1>
<?php Pjax::end(); ?>

但是如果超链接不在 <?php Pjax::begin(); 中怎么办??><?php Pjax::end();?>,就像:

but what if the hyperlink is not in the <?php Pjax::begin(); ?> <?php Pjax::end(); ?>, just like:

<nav>
<a href="">Click to refresh</a>
</nav>

<?php Pjax::begin(); ?>
<h1>Current time: <?= $time ?></h1>
<?php Pjax::end(); ?>

推荐答案

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

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

或更短的片段:

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

此外,在我的项目中,我使用了 Pjax 的覆盖版本:

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

这篇关于当超链接不在 pjax 中时,yii2 如何使用 pjax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 17:21