Yii2在弹出窗口或微调器中显示flash消息

时间:2017-10-14 06:02:16

标签: php jquery yii2 popup flash-message

有没有办法在弹出窗口或微调器中显示flash消息。现在我正在显示带有自定义jQuery的Flash消息以隐藏它。

在控制器中

\Yii::$app->getSession()->setFlash('error', 'Verification Failed..... Try GPRS Communication again!!');

在视图中

<div id="statusMsg" >
                     <?= Yii::$app->session->getFlash('error');?>
         </div>

$(document).ready(function(){
              setTimeout(function() {
                    $('#statusMsg').fadeOut('fast');
               }, 5000); // <-- time in milliseconds
      });

我想在弹出窗口或微调器中显示此Flash消息。

任何帮助都将受到高度赞赏

2 个答案:

答案 0 :(得分:0)

我通过Kartik扩展了警报小部件。并使用它。

  class AlertModal extends \yii\bootstrap\Widget
    {

...........
 public $alertTypes = [
        'modal-error' => 'alert-error',
        'modal-danger' => 'alert-danger',
        'modal-success' => 'alert-success',
        'modal-info' => 'alert-info',
        'modal-warning' => 'alert-warning'
    ];
    .........
        public function init()
        {
    ......

Modal::begin([
   'id' => 'modal-' . $this->options['id'],
   'header' => '<h2>warning</h2>',
   'toggleButton' => false,
   'clientOptions' => ['show' => true]
]);
/** original part  */
  echo \yii\bootstrap\Alert::widget([
      'body' => $message,
     'closeButton' => false,
      'options' => $this->options,
      ]);
/** \original part */
  Modal::end();

.....
}
...
}

布局:

 <?= \app\components\widgets\AlertModal::widget() ?>
 <?= Alert::widget() ?>  
控制器中的

Yii::$app->session->setFlash('modal-danger', 'you message');

答案 1 :(得分:0)

可以使用Kartik Growl扩展程序:posix_spawn()

查看布局/主要

use kartik\growl\Growl;
<body>
.....
   <?php foreach (Yii::$app->session->getAllFlashes() as $message):;  ?>
        <?php

        echo Growl::widget([
            'type' => (!empty($message['type'])) ? $message['type'] : 'danger',
            'title' => (!empty($message['title'])) ? Html::encode($message['title']) : 'Title Not Set!',
            'icon' => (!empty($message['icon'])) ? $message['icon'] : 'fa fa-info',
            'body' => (!empty($message['message'])) ? Html::encode($message['message']) : 'Message Not Set!',
            'showSeparator' => true,
            'delay' => 1, //This delay is how long before the message shows
            'pluginOptions' => [
                'delay' => (!empty($message['duration'])) ? $message['duration'] : 3000, //This delay is how long the message shows for
                'placement' => [
                    'from' => (!empty($message['positonY'])) ? $message['positonY'] : 'top',
                    'align' => (!empty($message['positonX'])) ? $message['positonX'] : 'right',
                ]
            ],
            'useAnimation'=>true
        ]);
        ?>
    <?php endforeach; ?>
</body>

Controller.php这样

public function actionSome(){
  .......your code here
  Yii::$app->getSession()->setFlash('success', [
                             'type' => 'success',
                             'duration' => 5000,
                             'icon' => 'glyphicon glyphicon-ok',
                             'message' => ' your Message',
                             'title' => 'Title of pop up',
                             'positonY' => 'top',
                             'positonX' => 'right'
                         ]);
    return $this->render('yourview');
}