Yii2 Kartik SideNav如何设置'活动'选项

时间:2016-12-17 22:19:49

标签: yii2 yii2-advanced-app kartik-v

我让Kartik的SideNav(http://demos.krajee.com/sidenav-demo/profile/default)通过将url设置为控制器/操作正常工作,但是不明白如何设置活动选项以使菜单保持打开并且选择正确的菜单项。

我尝试过设置'active'=> '控制器/动作'(见下文)以及 'active'=> '控制器/动作/默认#demo'但无济于事。当我将其设置为'employee / employee-dashboard / default#demo'时,它会确保突出显示一个选项卡,但是当我在这些选项卡上以相同方式执行时,不会更改其他选项卡。原件设置为'active'=>我认为'家'只是行动,但对我来说也不起作用。

            $type = SideNav::TYPE_DEFAULT;
            $heading = '<i class="glyphicon glyphicon-cog"></i> Employee Menu';
            $item = '/employee/employee-dashboard/default#demo';

echo SideNav::widget([
                'type' => $type,
                'encodeLabels' => false,
                'heading' => $heading,
                'items' => [
                    // Important: you need to specify url as 'controller/action',
                    // not just as 'controller' even if default action is used.
                    //
                    // NOTE: The variable `$item` is specific to this demo page that determines
                    // which menu item will be activated. You need to accordingly define and pass
                    // such variables to your view object to handle such logic in your application
                    // (to determine the active status).
                    //
                    ['label' => 'Dashboard', 'icon' => 'home', 'url' => Url::to(['/employee/employee-dashboard', 'type'=>$type]), 'active' => ($item == '/employee/employee-dashboard/default#demo')]

                    ['label' => 'Paycheck Info', 'icon' => 'book', 'items' => [
                        ['label' => '<span class="pull-right badge">10</span> W2 Forms', 'url' => Url::to(['/employee/w2-form', 'type'=>$type]), 'active' => ($item == 'w2-form')],

2 个答案:

答案 0 :(得分:2)

'active'=>($item == 'about')

where
    $item = Yii::$app->controller->action->id;


This is how i used

<?php

    $item = Yii::$app->controller->action->id;
    echo SideNav::widget([
        'type' => SideNav::TYPE_DEFAULT,
        'heading' => 'Options',
        'items' => [
            [
                'url' => Yii::$app->homeUrl,
                'label' => 'Home',
                'icon' => 'home',
                'active'=>($item == 'index')
            ],
            [
                'label' => 'Help',
                'icon' => 'question-sign',
                'items' => [
                    ['label' => 'About', 'icon'=>'info-sign', 'url'=>Yii::$app->homeUrl.'site/about', 'active'=>($item == 'about')],
                    ['label' => 'Contact', 'icon'=>'phone', 'url'=>Yii::$app->homeUrl.'site/contact', 'active'=>($item == 'contact')],
                ],
            ],
        ],
    ]);
?>

答案 1 :(得分:0)

如果要在每页的基础上设置活动菜单项,我找到的一个解决方案是:

使用SideNav小部件创建一个单独的视图文件(例如sidemenu.php),该小部件接受一个名为'currentPage'的参数。

echo SideNav::widget([
    'type' => SideNav::TYPE_DEFAULT,
    'heading' => 'Options',
    'items' => [
       [
          'url' => Url::toRoute('/site/index'),
          'label' => 'Home',
          'icon' => 'file',
          'active'=>($currentPage == 'home'),
       ],
       [
          'url' => Url::toRoute('/site/about'),
          'label' => 'About',
          'icon' => 'file',
          'active'=>($currentPage == 'about'),
       ],

然后在渲染菜单的视图页面中,设置currentPage属性 - 例如:

echo $this->render('sidemenu', ['currentPage'=>'home']);

或     echo $ this-&gt; render('sidemenu',['currentPage'=&gt;'about']);