teamoping 2016-08-11 14:13:35 10054次浏览 0条评论 15 7 0

该组件提供五种类型提示,总的样式有四种,提示类型有:error,danger,success,info,warning;样式有:alert-danger,lert-success,alert-info,alert-warning ;

使用方法:在控制器中加入:

\Yii::$app->getSession()->setFlash('success', "头像设置成功!");

视图页面中引用:

use app\components\widgets\Alert;
<?= Alert::widget() ?>

其实在控制器中没多大作用,就是在类中添加,可以很好做到提示作用;

具体 Alert.php 代码 如下:

<?php      
namespace app\components\widgets;        
        
class Alert extends \yii\bootstrap\Widget
{
            
    public $alertTypes = [
        'error'   => 'alert-danger',
        'danger'  => 'alert-danger',
        'success' => 'alert-success',
        'info'    => 'alert-info',
        'warning' => 'alert-warning'
    ];
        
public $closeButton = [];
        
public function init()
{
    parent::init();
        
    $session = \Yii::$app->getSession();
    $flashes = $session->getAllFlashes();
    $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
        
    foreach ($flashes as $type => $data) {
    if (isset($this->alertTypes[$type])) {
        $data = (array) $data;
        foreach ($data as $message) {
        /* initialize css class for each alert box */
        $this->options['class'] = $this->alertTypes[$type] . $appendCss;
        
        /* assign unique id to each alert box */
        $this->options['id'] = $this->getId() . '-' . $type;
        
        echo \yii\bootstrap\Alert::widget([
            'body' => $message,
            'closeButton' => $this->closeButton,
            'options' => $this->options,
        ]);
        }
        
        $session->removeFlash($type);
        }
        }
    }
}
觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册