Yii - YiiStrap - TbNavbar - 设置活动菜单项

时间:2014-08-18 21:02:48

标签: php twitter-bootstrap yii

我在我的网站上使用了Yiistrap(http://www.getyiistrap.com/)。我在我的主题中使用TbNavbar小部件。但是,我似乎无法根据您所在的页面弄清楚各种菜单项应该如何变为活动状态。是应该根据您所在的URL自动发生,还是我必须编写一个查看当前URL并明确将给定菜单项设置为活动的函数?

$arNavbar = array(
  'brandLabel' => false,
  'collapse' => true,
  'display' => null, // default is static to top
  'items' => array(
    array(
        'class' => 'bootstrap.widgets.TbNav',
        'items' => array(
            array('label' => 'Home', 'url' => '/', 'active' => true),
            array('label' => 'Docs', 'url' => '/docs'),
            array('label' => 'Stuff', 'url' => '/stuff'),
            array('label' => 'Things', 'url' => '/things'),
            array('label' => 'About', 'url' => '/about'),
        ),
    ),
  ),
);

主页始终处于活动状态

....
        'items' => array(
            array('label' => 'Home', 'url' => '/'),
            array('label' => 'Docs', 'url' => '/docs'),
            array('label' => 'Stuff', 'url' => '/stuff'),
            array('label' => 'Things', 'url' => '/things'),
            array('label' => 'About', 'url' => '/about'),
        ),
....

什么都没有活动

1 个答案:

答案 0 :(得分:2)

将URL-s添加为数组。在这种情况下,第一个数组元素引用控制器路由,其余的键值对引用URL的附加GET参数。

例如:
array('index','param1'=>2)是指向'指数的路径。当前控制器的动作。 array('site/index','param1'=>2)是指向'指数的路径。网站的行动'控制器。

所以在你的情况下使用这个:

'items' => array(
        array('label' => 'Home', 'url' => array('controller/home')),
        array('label' => 'Docs', 'url' => array('controller/docs', 'document_id'=>4)),
        ...
    ),

这样,如果您在指定的控制器/操作中,菜单项将处于活动状态 或者,您可以告诉“活跃的'参数为' true'在指定的控制器或动作这样:

'items' => array(
        // This will be active only on the 'site/home' action - this is the default if you give the URL in an array format
        array(
            'label' => 'Home',
            'url' => array('site/home'),
            'active' => (Yii::app()->controller->id == 'site' && Yii::app()->controller->action->id == 'home') ? true : false,
        ),

        // this will be active at every action of the 'docs' controller
        array(
            'label' => 'Docs',
            'url' => array('docs/index'),
            'active' => (Yii::app()->controller->id == 'docs') ? true : false,
        ),

        // this will be active at the 'view' action of every controller
        array(
            'label' => 'Stuff',
            'url' => array('stuff/index'),
            'active' => (Yii::app()->controller->action->id == 'view') ? true : false,
        ),
        ...
    ),

快乐的编码!

相关问题