Yii2.0 与Pjax基本用法 [ 2.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');
}
备注:
晦涩de咚
注册时间:2015-08-03
最后登录:2020-09-04
在线时长:356小时20分
最后登录:2020-09-04
在线时长:356小时20分
- 粉丝36
- 金钱13598
- 威望340
- 积分20558
共 2 条评论
如果a标签与内容不在同一个Pjax::begin 里面怎么处理?
同问。试了下,似乎时不可以的
@lvbaocheng 给Pjax增加一个ID,然后用jquery做刷新,这样连按钮都不用了。
比如:
Pjax::begin(['id'=>'test]);
......
Pjax::end();
js:
$.pjax({url: url, container: '#test'});
也可以用Pjax reload:
$.pjax.reload('#test')
谢谢了@Lina
mark ,非常感谢分享