晦涩de咚 2016-06-24 14:56:02 11204次浏览 2条评论 7 1 0

Refresh

views\site\index.php:

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

controllers\SiteController.php:

public function actionIndex()
{
    return $this->render('index', ['time' => date('H:i:s')]);
}

Auto Refresh

views\site\auto-refresh.php:

<?php
$script = <<< JS
$(document).ready(function() {
    setInterval(function(){ $("#refreshButton").click(); }, 3000);
});
JS;
$this->registerJs($script);
?>

<?php
$script = <<< JS
$(document).ready(function() {
    setInterval(function(){ $("#refreshButton").click(); }, 3000);//每隔3秒自动刷新
});
JS;
$this->registerJs($script);
?>

controllers\SiteController.php:

public function actionAutoRefresh()
{
    return $this->render('auto-refresh', ['time' => date('H:i:s')]);
}

Time/Date

views\site\time-date.php:

<?php Pjax::begin(); ?>
<?= Html::a("Show Time", ['site/time'], ['class' => 'btn btn-lg btn-primary']) ?>
<?= Html::a("Show Date", ['site/date'], ['class' => 'btn btn-lg btn-success']) ?>
<h1>It's: <?= $response ?></h1>
<?php Pjax::end(); ?>

controllers\SiteController.php:

public function actionTime()
{
    return $this->render('time-date', ['response' => date('H:i:s')]);
}

public function actionDate()
{
    return $this->render('time-date', ['response' => date('Y-M-d')]);
}

Multiple

views\site\multiple.php:

<div class="col-sm-12 col-md-6">
    <?php Pjax::begin(); ?>
    <?= Html::a("Generate Random String", ['site/multiple'], ['class' => 'btn btn-lg btn-primary']) ?>
    <h3><?= $randomString ?></h3>
    <?php Pjax::end(); ?>
</div>

<div class="col-sm-12 col-md-6">
    <?php Pjax::begin(); ?>
    <?= Html::a("Generate Random Key", ['site/multiple'], ['class' => 'btn btn-lg btn-primary']) ?>
    <h3><?= $randomKey ?><h3>
    <?php Pjax::end(); ?>
</div>

controllers\SiteController.php:

public function actionMultiple()
{
    $security = new Security();
    $randomString = $security->generateRandomString();
    $randomKey = $security->generateRandomKey();
    return $this->render('multiple', [
        'randomString' => $randomString,
        'randomKey' => $randomKey,
    ]);
}

From submission

views\site\form-submission.php:

<?php Pjax::begin(); ?>
<?= Html::beginForm(['site/form-submission'], 'post', ['data-pjax' => '', 'class' => 'form-inline']); ?>
    <?= Html::input('text', 'string', Yii::$app->request->post('string'), ['class' => 'form-control']) ?>
    <?= Html::submitButton('Hash String', ['class' => 'btn btn-lg btn-primary', 'name' => 'hash-button']) ?>
<?= Html::endForm() ?>
<h3><?= $stringHash ?></h3>
<?php Pjax::end(); ?>

controllers\SiteController.php:

public function actionFormSubmission()
{
    $security = new Security();
    $string = Yii::$app->request->post('string');
    $stringHash = '';
    if (!is_null($string)) {
        $stringHash = $security->generatePasswordHash($string);
    }
    return $this->render('form-submission', [
        'stringHash' => $stringHash,
    ]);
}

Vote

views\site\vote.php:

<?php Pjax::begin(['enablePushState' => false]); ?>
<?= Html::a('', ['site/upvote'], ['class' => 'btn btn-lg btn-warning glyphicon glyphicon-arrow-up']) ?>
<?= Html::a('', ['site/downvote'], ['class' => 'btn btn-lg btn-primary glyphicon glyphicon-arrow-down']) ?>
<h1><?= Yii::$app->session->get('votes', 0) ?></h1>
<?php Pjax::end(); ?>

controllers\SiteController.php:

public function actionVote()
{
    return $this->render('vote');
}

public function actionUpvote()
{
    $votes = Yii::$app->session->get('votes', 0);
    Yii::$app->session->set('votes', ++$votes);
    return $this->render('vote');
}

public function actionDownvote()
{
    $votes = Yii::$app->session->get('votes', 0);
    Yii::$app->session->set('votes', --$votes);
    return $this->render('vote');
}

备注:

原文链接: http://blog.neattutorials.com/examples/pjax/web/

觉得很赞
  • 评论于 2016-08-05 11:57 举报

    如果a标签与内容不在同一个Pjax::begin 里面怎么处理?

    3 条回复
    评论于 2017-10-11 14:08 回复

    同问。试了下,似乎时不可以的

    评论于 2018-01-02 08:54 回复

    给Pjax增加一个ID,然后用jquery做刷新,这样连按钮都不用了。
    比如:
    Pjax::begin(['id'=>'test]);
    ......
    Pjax::end();

    js:
    $.pjax({url: url, container: '#test'});

    也可以用Pjax reload:
    $.pjax.reload('#test')

    评论于 2018-01-08 10:45 回复

    谢谢了@Lina

  • 评论于 2019-01-24 13:54 举报

    mark ,非常感谢分享

您需要登录后才可以评论。登录 | 立即注册