Yii2 Kartik SideNav How to set 'active' option

Web Tips Q & ACategory: Krajee Yii2 ExtensionsYii2 Kartik SideNav How to set 'active' option
markopolo asked 1 year ago

I have the SideNav widget working correctly except for the ‘active’ option that controls the menu’s active selection.  On the complete demo page (http://demos.krajee.com/sidenav-demo/profile/default) it shows that the ‘active’ option is set to what looks like the action of the controller….i.e. site/home is set to ‘active’ => ($item == ‘home’) but I cannot get this to work correctly.

Here is my code showing me trying two different ways of setting ‘active’.  I’ve also tried setting the variable $item = ‘/employee/employee-dashboard’ without the ‘/default#demo’.  

Start your code here
  1. <?php
  2.                 $type = SideNav::TYPE_DEFAULT;
  3.                 $heading = ‘<i class=”glyphicon glyphicon-cog”></i> Employee Menu’;
  4.                 $item = ‘/employee/employee-dashboard/default#demo’;
  5.                 echo SideNav::widget([
  6.                     ‘type’ => $type,
  7.                     ‘encodeLabels’ => false,
  8.                     ‘heading’ => $heading,
  9.                     ‘items’ => [
  10.                         // Important: you need to specify url as ‘controller/action’,
  11.                         // not just as ‘controller’ even if default action is used.
  12.                         //
  13.                         // NOTE: The variable `$item` is specific to this demo page that determines
  14.                         // which menu item will be activated. You need to accordingly define and pass
  15.                         // such variables to your view object to handle such logic in your application
  16.                         // (to determine the active status).
  17.                         //
  18.                         [‘label’ => ‘Dashboard’, ‘icon’ => ‘home’, ‘url’ => Url::to([‘/employee/employee-dashboard’, ‘type’=>$type]), ‘active’ => ($item == ‘/employee/employee-dashboard/default#demo’)],
  19.                         [‘label’ => ‘Paycheck Info’, ‘icon’ => ‘book’, ‘items’ => [
  20.                             [‘label’ => ‘<span class=”pull-right badge”>10</span> W2 Forms’, ‘url’ => Url::to([‘/employee/w2-form’, ‘type’=>$type]), ‘active’ => ($item == ‘w2-form’)],

markopolo replied 1 year ago

I’m posting my own answer in hopes of helping someone else. See this SO article for the answer: http://stackoverflow.com/questions/41203627/yii2-kartik-sidenav-how-to-set-active-option/41220864#41220864

Login or register to contribute or comment.

Login   Register