一次显示两个视图,并在更改时更改URL

时间:2021-03-29 11:58:44

I have a main view with a menu which helps me display another view. It's similar to this:

我有一个菜单的主视图,可以帮助我显示另一个视图。它与此类似:

<div id="page">
    <div id="menu">
            <a href="controller/page1">Page1</a>
            <a href="controller/page2">Page2</a>
    </div>
    <div id="content">
            <!-- Page1 or Page2 are displayed here -->
    </div>
</div>

I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2

我正在使用php的Yii框架。这让我不使用 。所以我正在寻找一个不同的解决方案。我可以用Ajax做到这一点,但我也希望链接更改为mypage / controller / Page2。使用Ajax我只能得到它:mypage / controller / index#Page2

1 个解决方案

#1


0  

in main view, instead of include do

在主视图中,而不是包含do

<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>

UPDATE:

protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking

您喜欢的protected / views / controller / page1.php和protected / views / controller / page2.php内容

protected/views/layouts/custom.php:

<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
    <div id="menu">
        <?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
        <?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
    </div>
    <div id="content">
        <?php echo $content; ?>
    </div>
</div>
<?php $this->endContent(); ?>

protected/controllers/ControllerController.php:

class ControllerController extends Controller {

    /**
     * @var string the default layout for the views.
     */
    public $layout = '//layouts/custom';

    public function actionPage1() {
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('page1');
        else
            $this->render('page1');
    }

    public function actionPage2() {
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('page2');
        else
            $this->render('page2');
    }

}

UPDATE2:

If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>

如果您需要更改地址栏中的链接,那么您唯一的选择是使用常规链接而不是ajax

using ajax the preferred way is using hash like you mentioned.

使用ajax首选方法是使用你提到的哈希。

#1


0  

in main view, instead of include do

在主视图中,而不是包含do

<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>

UPDATE:

protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking

您喜欢的protected / views / controller / page1.php和protected / views / controller / page2.php内容

protected/views/layouts/custom.php:

<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
    <div id="menu">
        <?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
        <?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
    </div>
    <div id="content">
        <?php echo $content; ?>
    </div>
</div>
<?php $this->endContent(); ?>

protected/controllers/ControllerController.php:

class ControllerController extends Controller {

    /**
     * @var string the default layout for the views.
     */
    public $layout = '//layouts/custom';

    public function actionPage1() {
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('page1');
        else
            $this->render('page1');
    }

    public function actionPage2() {
        if (Yii::app()->request->isAjaxRequest)
            $this->renderPartial('page2');
        else
            $this->render('page2');
    }

}

UPDATE2:

If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>

如果您需要更改地址栏中的链接,那么您唯一的选择是使用常规链接而不是ajax

using ajax the preferred way is using hash like you mentioned.

使用ajax首选方法是使用你提到的哈希。