如何使用yii2 -bootstrap扩展名将模式添加到yii2中的导航栏?

时间:2022-04-05 06:59:37

I'm trying to put modal in a navbar in my yii2 project. I'm using yii2-bootstrap extension.

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

Code for my nav:

导航代码:

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


    ?>

code for modal:

模态代码:

<?php

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

 echo 'Say hello...';

 Modal::end();
?>

can anyone please tell me how to add this modal to navbar?

任何人都可以告诉我如何将此模式添加到导航栏?

2 个解决方案

#1


7  

First place the modal with an id on your site/index

首先在您的站点/索引上放置带有id的模态

    <?php
     use yii\bootstrap\Modal;

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

         echo "Say Hello...";

     Modal::end();
    ?>

Then create a jQuery action in your controller/SiteController

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

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

Finally in views\layouts\main add the link in your Nav::wigdet

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

['label' => 'Show Modal', 'url' => ['/site/showmodal']],

#2


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>', 

    ],

     ]
    ];

And for the modal widget:

对于模态小部件:

 <?php

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

 Modal::end();

?>

#1


7  

First place the modal with an id on your site/index

首先在您的站点/索引上放置带有id的模态

    <?php
     use yii\bootstrap\Modal;

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

         echo "Say Hello...";

     Modal::end();
    ?>

Then create a jQuery action in your controller/SiteController

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

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

Finally in views\layouts\main add the link in your Nav::wigdet

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

['label' => 'Show Modal', 'url' => ['/site/showmodal']],

#2


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>', 

    ],

     ]
    ];

And for the modal widget:

对于模态小部件:

 <?php

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

 Modal::end();

?>