簡體   English   中英

yii2超鏈接不在pjax時如何使用pjax

[英]yii2 how to use pjax when hyperlink is not in pjax

要在yii2中使用pjax,就像:

<?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(); ?> <?php Pjax::begin(); ?> <?php Pjax::end(); ?> <?php Pjax::begin(); ?> <?php Pjax::end(); ?> ,就像:

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

像這樣:

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

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

有效!

我認為這可以解決你的問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM