wolailk 2017-02-28 13:58:02 13058次浏览 2条回复 2 4 0

<?php

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        //['class' => 'yii\grid\SerialColumn'],不需要显示前面的导航
        [
            'attribute' => 'id',
            'headerOptions' => ['width' => '100']
        ],
        [
            'attribute' => 'uid',
            'label' => '用户ID',
            'value' => function($model) {
                $user = Users::findOne(['id' => $model->uid]);
                return '[' . $user->id . ']' . $user->username;
            },
                    'headerOptions' => ['width' => '200']
                ],
                'title',
                [
                    'attribute' => 'status',
                    'label' => '状态',
                    'value' => function($model) {
                        return $model->status == 1 ? "开启" : "关闭";
                    },
                    'headerOptions' => ['width' => '100']
                ],
                ['class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{view} {update} {delete}',
                    'buttons' => [
                        'view' => function ($url, $model) {
                            $url = "/qa/view?id=" . $model->id;
                            return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, ['title' => '查看', 'target' => '_blank']);
                        },
                                'update' => function ($url, $model) {
                            return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, ['title' => '编辑', 'target' => '_blank']);
                        },
                            ],
                            'headerOptions' => ['width' => '70']
                        ],
                    ],
                    'emptyText' => '没有筛选到任何内容哦',
                ]);
                ?>

1、覆写一个栏目,比如覆写id栏目

[ 'attribute' => 'id', 'headerOptions' => ['width' => '100'] ],

2、覆写一个栏目,并修改其值

[ 'attribute' => 'status', 'label' => '状态', 'value' => function($model) {

return $model->status == 1 ? "开启" : "关闭";

}, 'headerOptions' => ['width' => '100'] ],

3、重写默认的查看、编辑和删除,增加其他功能按钮

在template中{up}那么就可以增加一个up按钮,当然你的up需要定义

['class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{view} {update} {delete}',

                    'buttons' => [
                        'view' => function ($url, $model) {
                            $url = "/qa/view?id=" . $model->id;
                            return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, ['title' => '查看', 'target' => '_blank']);
                        },
                                'update' => function ($url, $model) {
                            return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, ['title' => '编辑', 'target' => '_blank']);
                        },
                            ],
                            'headerOptions' => ['width' => '70']
                        ],
                    ],

4、没有搜索到结果的时候显示提示

'emptyText' => '没有筛选到任何内容哦',

觉得很赞
  • 回复于 2017-06-19 16:27 举报

    排版要在醒目些就更好了,谢了,收藏了

  • 回复于 2017-10-21 17:46 举报

    为什么我加上这句:'emptyText' => '没有筛选到任何内容哦', 会有错误,
    错误提示:
    Getting unknown property: backend\models\Article::没有筛选到任何内容哦

您需要登录后才可以回复。登录 | 立即注册