【问题标题】:Yii2 Kartik SideNav How to set 'active' optionYii2 Kartik SideNav 如何设置“活动”选项
【发布时间】:2016-12-19 18:54:26
【问题描述】:

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

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

            $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')],

【问题讨论】:

  • 我只是想澄清一下。您想将菜单行(这是一个下拉菜单)设置为 active 并且它应该与所有子项保持打开状态?
  • 是的。有关正确操作,请参阅demos.krajee.com/sidenav-demo/profile/default 的演示页面。目前,我可以单击任何菜单项并转到正确的页面,但菜单项不会保持突出显示,如果它是带有子项的扩展菜单,则菜单会关闭。谢谢。

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


【解决方案1】:
'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')],
                ],
            ],
        ],
    ]);
?>

【讨论】:

  • 谢谢!这是一个非常优雅的解决方案,它允许您从一个布局文件中为许多页面使用它。我发现的唯一其他解决方案(就在几分钟前),我将在下面发布,强制您在单个页面上使用它。
【解决方案2】:

如果您想按页面设置活动菜单项,我发现的一个解决方案是:

仅使用 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->render('sidemenu', ['currentPage'=>'about']);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 2018-05-23
    相关资源
    最近更新 更多