如何使用yii2 -bootstrap扩展名在yii2中向navbar添加模态?

时间:2014-03-24 04:55:04

标签: php jquery css twitter-bootstrap yii2

我正在尝试将模态放在我的yii2项目的导航栏中。我正在使用yii2-bootstrap扩展。

我的导航代码:

        NavBar::begin([
            'brandLabel' => 'My Company',
            'brandUrl' => Yii::$app->homeUrl,
            'options' => [
                'class' => 'navbar-inverse navbar-fixed-top',
            ],
        ]);
        $menuItems = [
            ['label' => 'Home', 'url' => ['/site/index']],
            //['label' => 'facilities', 'url' => ['/facilities/index']],

        ['label' => 'Hotel',
        'items' => [
             ['label' => 'Facilities', 'url' => ['/facilities/index']],
           //  '<li class="divider"></li>',
         //    '<li class="dropdown-header">Dropdown Header</li>',
             ['label' => 'Cuisines', 'url' => ['/cuisines/index']],
        ],

         ]
        ];

        if (Yii::$app->user->isGuest) {
            $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
        } else {
            $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();


    ?>

模态代码:

<?php

Modal::begin([
    'header' => '<h2>Hello world</h2>',
    'toggleButton' => ['label' => 'click me'],
 ]);

 echo 'Say hello...';

 Modal::end();
?>

有人可以告诉我如何将这个模态添加到导航栏吗?

2 个答案:

答案 0 :(得分:7)

首先在您的网站/索引

上放置带有ID的模式
    <?php
     use yii\bootstrap\Modal;

     Modal::begin(['id' => 'modal',
        'header' => '<h2>Hello world</h2>']);

         echo "Say Hello...";

     Modal::end();
    ?>

然后在您的controller / SiteController中创建一个jQuery动作

function actionShowmodal(){
    $js='$("#modal").modal("show")';
    $this->getView()->registerJs($js);        
    return $this->render('index');
}

最后在views \ layouts \ main中添加Nav :: wigdet

中的链接
['label' => 'Show Modal', 'url' => ['/site/showmodal']],

答案 1 :(得分:2)

    $menuItems = [
        ['label' => 'Home', 'url' => ['/site/index']],
        //['label' => 'facilities', 'url' => ['/facilities/index']],

    ['label' => 'Hotel',
    'items' => [
         ['label' => 'Facilities', 'url' => ['/facilities/index']],
       //  '<li class="divider"></li>',
     //    '<li class="dropdown-header">Dropdown Header</li>',
         ['label' => 'Cuisines', 'url' => ['/cuisines/index']],
     // insert this line
         '<li><a data-toggle="modal" data-target="#modal" style="cursor: pointer;">Click me gently!</a></li>', 

    ],

     ]
    ];

对于模态小部件:

 <?php

 Modal::begin([
   'header' => '<h2>Hello world</h2>',
   'toggleButton' => ['label' => 'click me'],
   'id' => 'modal', // <-- insert this modal's ID
 ]);
 echo 'Say hello...';

 Modal::end();

&GT;