Yii2 Kartik SideNav How to set 'active' option

1.2k views Asked by At

I have Kartik's SideNav (http://demos.krajee.com/sidenav-demo/profile/default) working properly by setting the url's to controller/action, but don't understand how to set the active option so that the menu stays open and the correct menu item stays selected.

I've tried setting 'active'=> 'controller/action' (see below) as well as 'active' => 'controller/action/default#demo' but to no avail. When I do set it as 'employee/employee-dashboard/default#demo' it does keep that one tab highlighted but won't change the others when I do it the same way on those tabs. The original was set to 'active'=> 'home' which I assume is just the action but that didn't work for me either.

            $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

There are 2 answers

1
abdulwadood On BEST ANSWER
'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')],
                ],
            ],
        ],
    ]);
?>
0
Mark C. On

One solution I found if you want to set the active menu item on a per page basis is:

Create a separate view file (e.g. sidemenu.php) with just the SideNav widget, which accepts a parameter called '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'),
       ],

Then in the view page where you are rendering the menu, set the currentPage property -- for example:

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

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