简体   繁体   English

yii2中的下拉菜单不起作用

[英]Dropdown in yii2 Not working

I'm trying to bring dropdown in my header. 我正在尝试在标题中添加下拉菜单。 Dropdown label coming. 下拉标签来了。 But, dropdwon values not coming. 但是,dropwwon值不会到来。 What may be the problem ? 可能是什么问题?

<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\bootstrap\Dropdown;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;

AppAsset::register($this);
?>

<div class="wrap">
<?php
    NavBar::begin([
    'brandLabel' => 'My Company',
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar-inverse navbar-fixed-top',
    ],
    ]);?>

<?
    echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'items' => [
        ['label' => 'Danish Enam', 'url' => ['/site/register']],
        ['label' => 'Dropdown', 'url' => ['#'],
         ['label' => 'DropdownA', 'url' => '/'],
             ['label' => 'DropdownB', 'url' => '#'],
        ],
    ],
    ]);
NavBar::end();
?>

Here is screenshot. 这是截图。 You all can clearly see. 你们都可以清楚地看到。 Dropdown coming in header. 下拉列表中出现标题。 Right to 'My Company' Text. “我的公司”文本的权利。 But, no values are coming. 但是,没有价值来临。 Not clickable. 无法点击。 在此处输入图片说明

Any Idea ? 任何想法 ?

在此处输入图片说明

You should simply use another Nav widget instead of Dropdown : 您应该只使用另一个Nav小部件而不是Dropdown

NavBar::begin([
    'brandLabel' => 'My Company',
    'brandUrl' => Yii::$app->homeUrl,
    'options' => [
        'class' => 'navbar-inverse navbar-fixed-top',
    ],
]);

echo Nav::widget([
    'options' => [
        'class' => 'navbar-nav navbar-left',
    ],
    'items' => [
        [
            'label' => 'Dropdown',
            'url' => '#',
            'items' => [
                ['label' => 'DropdownA', 'url' => '/'],
                ['label' => 'DropdownB', 'url' => '#'],
            ],
        ],
    ],
]);

Please try this code : 请尝试以下代码:

echo '<ul id="navbar-id" class="navbar-nav navbar-right nav">';
echo '<li class="dropdown">';
echo '<a href="#" data-toggle="dropdown" class="dropdown-toggle">Label <b class="caret"></b></a>';

echo Dropdown::widget([
    'items' => [
        ['label' => 'DropdownA', 'url' => '/'],
        ['label' => 'DropdownB', 'url' => '#'],
    ],
]);

echo '</li>';
echo '</ul>';
<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

<div class="wrap">
    <?php
    NavBar::begin([
        'brandLabel' => 'My Company',
        'brandUrl' => Yii::$app->homeUrl,
        'options' => [
            'class' => 'navbar-inverse navbar-fixed-top',
        ],
    ]);


    echo Nav::widget([
        'items' => [
            [
                'label' => 'Home',
                'url' => ['site/index'],
                'linkOptions' => ['data-method' => 'post'],
            ],
            [
                'label' => 'Dropdown',
                'items' => [
                     ['label' => 'Level 1 - Dropdown A', 'url' => '#'],
                     '<li class="divider"></li>',

                     ['label' => 'Level 1 - Dropdown B', 'url' => '#'],
                ],
            ],
            [
                'label' => 'Login',
                'url' => ['site/login'],
                'visible' => Yii::$app->user->isGuest
            ],
        ],
        'options' => ['class' =>'nav-pills'], // set this to nav-tab to get tab-styled navigation
    ]);


    NavBar::end();
    ?>

    <div class="container">
        <?= Breadcrumbs::widget([
            'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
        ]) ?>
        <?= $content ?>
    </div>
</div>

<footer class="footer">
    <div class="container">
        <p class="pull-left">&copy; My Company <?= date('Y') ?></p>

        <p class="pull-right"><?= Yii::powered() ?></p>
    </div>
</footer>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

this is an entire layout code. 这是一个完整的布局代码。 try this layout for any view it will surely work. 尝试此布局以查看肯定会起作用的任何视图。 first name this file header.php(any name is ok) then select the view file for which u want this layout with dropdown. 首先将此文件命名为header.php(任何名称都可以),然后选择要使用下拉菜单进行布局的视图文件。

small error in your code : 您的代码中的小错误:

NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
    'class' => 'navbar-inverse navbar-fixed-top',
],
]);


echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
    ['label' => 'Danish Enam', 'url' => ['/site/register']],
    ['label' => 'Dropdown',
        'items' => [
            ['label' => 'DropdownA', 'url' => '/'],
            ['label' => 'DropdownB', 'url' => '#'],
        ],
    ],
],
]);

NavBar::end();  

Then the bad new, there is a limitation with bootstrap, you can only have 2 levels for the dropdown not 3. 然后是不好的新消息,引导程序有一个限制,下拉列表只能有2个级别,而下拉列表不能为3个级别。

If you want more information take a look there : 如果您想了解更多信息,请查看:

Multilevel Boostrap Menu in Yii 2 Yii 2中的多级Boostrap菜单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM