首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >yii2当超链接不在pjax中时如何使用pjax

yii2当超链接不在pjax中时如何使用pjax
EN

Stack Overflow用户
提问于 2016-08-25 16:12:04
回答 3查看 2.1K关注 0票数 3

要在yii2中使用pjax,就像这样:

代码语言:javascript
复制
<?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(); ?>中,该怎么办:

代码语言:javascript
复制
<nav>
<a href="">Click to refresh</a>
</nav>

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

回答 3

Stack Overflow用户

发布于 2016-08-26 23:34:36

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

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

或更短的代码片段:

代码语言:javascript
复制
$.pjax.reload('#pjaxId', {timeout : false});

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

代码语言:javascript
复制
/**
 * 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;
}
票数 2
EN

Stack Overflow用户

发布于 2016-08-25 17:24:27

就像这样:

代码语言:javascript
复制
<?= Html::a("Refresh", ['site/index'], ['class' => 'btn btn-lg btn-primary']); ?>
<?php Pjax::begin(); ?>

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

啊,真灵!

票数 0
EN

Stack Overflow用户

发布于 2016-08-26 16:49:22

我认为这可以解决你的问题。

代码语言:javascript
复制
<span class="my-btn  btn btn-lg btn-danger">refresh</span>
<a href="javascript:function() { return false; }" class="my-btn btn btn-lg btn-primary"> refresh a</a>

<?php

$this->registerJs(
   '$("document").ready(function(){ 
        $(".my-btn").on("click", function() {
            $.pjax.reload({container:"#greet"});
        });
    });'
);
?>
<?php yii\widgets\Pjax::begin(['id'=>'greet']); ?>

<h1>Current time: <?= time() ?></h1>
<?php yii\widgets\Pjax::end(); ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39139958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档