yii2 带下拉菜单的导航栏

问题描述 投票:0回答:2

在默认导航栏上进行一些简单的编辑后,我得到了下面菜单的代码......如果我添加更多菜单,那将非常难看。

<?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();

        ?>

如何向任何菜单添加下拉菜单或子菜单?

navbar yii2-advanced-app
2个回答
36
投票

来自官方文档。我得到了答案。我将选项从

nav-pills
更改为
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'],
]);

3
投票

好吧,我没有所需的声誉,但是,为了更正确地回答“nerison”答案,也添加导航类:像这些

'options' => ['class' =>'navbar-nav'],
© www.soinside.com 2019 - 2024. All rights reserved.