yii2 nav widget submenu class

nicky picture nicky · May 11, 2015 · Viewed 19.7k times · Source

I am using the adminLTE theme for bootstrap and it uses treeview-menu class in order to operate the submenu.

<?=Nav::widget([
            'options' => ['class' => 'sidebar-menu treeview'],
            'items' => [

                ['label' => 'Menu 1', 'url' => ['/a/index']],
                ['label' => 'Menu 2', 'url' => ['/custom-perks/index']],
                ['label' => 'Submenu',  'items' => [
                    ['label' => 'Action', 'url' => '#'],
                    ['label' => 'Another action', 'url' => '#'],
                    ['label' => 'Something else here', 'url' => '#'],
                    ],
                ],
            ],
        ]);
        ?>

I tried using: ['label' => 'Submenu', 'options' => ['class' => 'treeview-menu'], 'items' =>..

Which obviously does not work.

I noticed that Menu::widget has a submenuTemplate but when I used that it stopped pickup up the "active".

Is there a way I can change either the way the adminLTE call is being applied to treeview-menu (tried changing it in app.js to dropdown-menu but that didn't help) or re-assign the UL submenu class without going into the vendor code?

Line 65: \yii\bootstrap\Dropdown - function init()

Answer

nicky picture nicky · May 21, 2015

Ok so I have found a work around - use the Menu widget instead and enable the activateParents flag:

<?=\yii\widgets\Menu::widget([
'options' => ['class' => 'sidebar-menu treeview'],
'items' => [

    ['label' => 'Menu 1', 'url' => ['/a/index']],
    ['label' => 'Menu 2', 'url' => ['/link2/index']],
    ['label' => 'Submenu',  
        'url' => ['#'],
        'template' => '<a href="{url}" >{label}<i class="fa fa-angle-left pull-right"></i></a>',
        'items' => [
            ['label' => 'Action', 'url' => '#'],
            ['label' => 'Another action', 'url' => '#'],
            ['label' => 'Something else here', 'url' => '#'],
        ],
    ],
],
'submenuTemplate' => "\n<ul class='treeview-menu'>\n{items}\n</ul>\n",
'encodeLabels' => false, //allows you to use html in labels
'activateParents' => true,   ]);  ?>

Hopefully this helps others as well!