YII2学习笔记:GridView::widget的使用

<?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' => '没有筛选到任何内容哦',


你可能感兴趣的:(YII2学习笔记:GridView::widget的使用)