1

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

<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>

我正在使用 php 的 Yii 框架。这使我不使用<?php include("menuview.php"); ?>. 所以我正在寻找不同的解决方案。我可以用 Ajax 做到这一点,但我也希望将链接更改为 mypage/controller/Page2。使用 Ajax 我只能做到这一点:mypage/controller/index#Page2

4

1 回答 1

0

在主视图中,而不是 include do

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

更新:

protected/views/controller/page1.phpprotected/views/controller/page2.php内容随心所欲

受保护/视图/布局/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(); ?>

受保护/控制器/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');
    }

}

更新2:

如果您也需要更改地址栏中的链接,那么您唯一的选择是使用常规链接而不是 ajax<?php echo CHtml::link('Page1', array('controller/page1')); ?>

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

于 2012-04-19T15:53:06.060 回答