2

我正在尝试在我的 yii2 项目中将 modal 放在导航栏中。我正在使用 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();
?>

谁能告诉我如何将此模式添加到导航栏?

4

2 回答 2

7

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

    <?php
     use yii\bootstrap\Modal;

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

         echo "Say Hello...";

     Modal::end();
    ?>

然后在你的控制器/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']],
于 2014-05-24T07:47:18.047 回答
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();

?>

于 2014-11-17T17:04:02.407 回答