码迷,mamicode.com
首页 > 其他好文 > 详细

yii2 GridView 操作项重写

时间:2015-04-29 16:35:09      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

    [
            ‘header‘ => ‘操作‘,
            ‘class‘ => ‘yii\grid\ActionColumn‘,
            ‘template‘ => ‘{view} {update} {delete} {abc}‘, //{view} {update} {delete}系统定义的,{abc}自定义的
            ‘buttons‘ => [
                ‘view‘ => function ($url, $model, $key) {  //$url是系统定义的三个操作的url,$model为数据对象,$key为主键id
                    return $key == 1 ? null : \common\components\MyHelper::mybutton(‘/xx/xx?id=‘ . $key, ‘view‘, [‘title‘ => ‘标签的title属性‘]);
                },
                ‘delete‘ => function ($url, $model, $key) {
                    return $key == 1 ? null : \common\components\MyHelper::mybutton($url, ‘delete‘);
                },
                ‘abc‘=>function(){return ‘<a>abc</a>‘;}
            ]
        ],

MyHellper代码如下:

  /**
     * 生成操作按钮
     * @param $url
     * @param string $type
     * @param array $options
     * @return string
     */
    public static function mybutton($url, $type = ‘update‘, $options = [])
    {
        if ($type == ‘view‘) {
            return Html::a(‘<span class="glyphicon glyphicon-eye-open"></span>‘, $url, ArrayHelper::merge([
                ‘title‘     => Yii::t(‘yii‘, ‘View‘),
                ‘data-pjax‘ => ‘0‘,
            ], $options));
        }
        if ($type == ‘update‘) {
            return Html::a(‘<span class="glyphicon glyphicon-pencil"></span>‘, $url, ArrayHelper::merge([
                ‘title‘     => Yii::t(‘yii‘, ‘Update‘),
                ‘data-pjax‘ => ‘0‘,
            ], $options));
        }
        if ($type == ‘delete‘) {
            return Html::a(‘<span class="glyphicon glyphicon-trash"></span>‘, $url, ArrayHelper::merge([
                ‘title‘        => Yii::t(‘yii‘, ‘Delete‘),
                ‘data-confirm‘ => Yii::t(‘yii‘, ‘Are you sure you want to delete this item?‘),
                ‘data-method‘  => ‘post‘,
                ‘data-pjax‘    => ‘0‘,
            ], $options));
        }
    }

 

yii2 GridView 操作项重写

标签:

原文地址:http://www.cnblogs.com/benlightning/p/4466063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!