带有下拉菜单的yii2导航栏 [英] yii2 navbar with dropdown
本文介绍了带有下拉菜单的yii2导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在默认导航栏上进行了简单的编辑后,我得到了下面菜单的代码...如果添加更多菜单,将非常难看.
After some simple edit on the default navbar, I got the code for the menu below... It would be very ugly if I add more menus.
<?php
NavBar::begin([
'brandLabel' => 'My Project',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Contact', 'url' => ['/site/contact'],'visible'=>false],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
if(yii::$app->user->can('admin')){
$menuItems[] = ['label' => 'Users', 'url' => ['/users']];
}
$menuItems[] = ['label' => 'BUB Sub Projects', 'url' => ['/bub']];
$menuItems[] = ['label' => 'Incoming BUB', 'url' => ['/bubincoming']];
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
如何在任何菜单中添加下拉菜单或子菜单?
How to add a dropdown or submenus to any of the menu?
推荐答案
From the official documentation. I got the answer. I changed the options from nav-pills
to navbar-nav
echo Nav::widget([
'items' => [
[
'label' => 'Home',
'url' => ['site/index'],
'linkOptions' => [...],
],
[
'label' => 'Dropdown',
'items' => [
['label' => 'Level 1 - Dropdown A', 'url' => '#'],
'<li class="divider"></li>',
'<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Level 1 - Dropdown B', 'url' => '#'],
],
],
],
'options' => ['class' =>'navbar-nav'],
]);
这篇关于带有下拉菜单的yii2导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文