Pjax更新多个块Yii2

时间:2016-11-21 20:50:38

标签: php yii2 pjax

我学习了Yii2,我决定在网站上熟悉Pjax技术的工作:http://blog.neattutorials.com/yii2-pjax-tutorial/。有“多个块”的例子。但它是作为示例版本实现的,并不完全正确。它写在下面。在此示例中,在一个操作 actionMultiple 中计算 string key ,但必须将其重新放入不同的操作中。所以我决定做对了,但是碰到了这个问题,当我点击链接时,它会将我重定向到一个新的页面,生成一个字符串或键。我需要在不重新加载的情况下在同一页面中进行。

控制器:

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

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

查看多个:

<?php
use yii\widgets\Pjax;
use yii\bootstrap\Html;

?>

<div class="col-sm-12 col-md-6">
    <?php Pjax::begin(); ?>
    <?= Html::a("Generate Random String", ['site/string'], ['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/key'], ['class' => 'btn btn-lg btn-primary']) ?>
    <h3><?= $randomKey ?><h3>
            <?php Pjax::end(); ?>
</div>

查看_randomString:

<?php
use yii\helpers\Html;

?>

<?= Html::a("Generate Random String", ['site/string'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomString ?></h3>

查看_randomKey:

<?php
use yii\helpers\Html;

?>

<?= Html::a("Generate Random Kay", ['site/key'], ['class' => 'btn btn-lg btn-primary']) ?>
<h3><?= $randomKey ?><h3>

请告诉我我做错了什么。

0 个答案:

没有答案