【问题标题】:yii2 navbar with dropdownyii2 带有下拉菜单的导航栏
【发布时间】:2015-07-24 05:45:27
【问题描述】:

在默认导航栏上进行一些简单的编辑后,我得到了下面菜单的代码......如果我添加更多菜单会很丑。

<?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


    【解决方案1】:

    来自官方documentation。我得到了答案。我将选项从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'],
    ]);
    

    【讨论】:

      【解决方案2】:

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

      'options' => ['class' =>'nav navbar-nav'],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-07
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多