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


转载于:https://my.oschina.net/kenblog/blog/419818

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值