I had constructed a bootstrap menu and now I want to reproduce it with Yii2 Nav widget. So, here is the initial state:
<nav class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="true">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">BrandLogo</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1" aria-expanded="true">
<ul class="nav navbar-nav">
<li>
<a href="#1" class="current">simple menu</a>
</li>
<li class="dropdown">
<a href="#4">dropdown menu <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>
<a href="#2">Submenu#1</a>
</li>
<li>
<a href="#3">Submenu#2</a>
</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" action="/action_page.php">
<div class="form-group has-feedback search">
<input type="text" class="form-control" placeholder="Search" />
<i class="glyphicon glyphicon-search form-control-feedback"></i>
</div>
</form>
</div>
</div>
</nav>
And here is how it looks like:
Now I would like to do the same menu with Nav widget. Here is the code:
NavBar::begin([
'brandLabel' => 'BrandLogo',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse',
],
]);
$menuItems = [
[
'label' => 'simple menu',
'url' => ['#1']
],
[
'label' => 'dropdown menu',
'url' => ['#4'],
'items' => [
[
'label' => 'Submenu#1',
'url' => ['#1'],
],
[
'label' => 'Submenu#2',
'url' => ['#2'],
],
]
],
[
'label' => '
<form class="navbar-form navbar-left" action="/action_page.php">
<div class="form-group has-feedback search">
<input type="text" class="form-control" placeholder="Search" />
<i class="glyphicon glyphicon-search form-control-feedback"></i>
</div>
</form>',
'encode' => false,
'url' => false,
],
];
if (count($menuItems)) {
echo Nav::widget([
'options' => ['class' => 'navbar-nav'],
'items' => $menuItems,
]);
}
NavBar::end();
The problem is that the results aren't equal. I found a few problems:
- The widget generates dropdown link as
<a class="dropdown-toggle" href="/main/#4" data-toggle="dropdown">dropdown menu <span class="caret"></span></a>
How can I get rid ofdata-toggle="dropdown"
andclass="dropdown-toggle"
? - The search form is wrapped into
<a></a>
tags. That is why the navbar is broken:How can I get rid of the unnecessary tag?
That is because you are not following the actual HTML structure, and according to that you need to add the form after the
ul
not inside theli
, but that form should be part of theNavBar
and if you look into the definationso just move your form outside the
$items
after you are calling theNav::widget()
and before you call theNavBar::end()
.And you can use the
linkOptions
to customize or remove the class or any other attribute related to the linkEDIT
if you are looking to completly remove the class name
dropdown-toggle
then you might have to override theyii\bootstrap\Nav::renderItems()
by extending the widget because it is added by default as the bootstrap class so you have to just copy therenderItems()
to your extended class and comment out the lineHtml::addCssClass ( $linkOptions , [ 'widget' => 'dropdown-toggle' ] );
which adds the class there and then change thenamespace
where you are calling theNav
fromyii\bootstrap\Nav::widget()
tocommon\components\Nav::widget()
Add the following class in your
common\components\
or if you plan to copy it somewhere else do update the namespace in the code