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' => '没有筛选到任何内容哦',
共 2 条回复
wolailk
注册时间:2015-03-26
最后登录:2020-06-09
在线时长:8小时8分
最后登录:2020-06-09
在线时长:8小时8分
- 粉丝8
- 金钱260
- 威望0
- 积分340