yii2-GridView在开发中常用的功能及技巧
来源:互联网 发布:杀马特火星文 知乎 编辑:程序博客网 时间:2024/06/05 23:00
数据网格或者说 GridView 小部件是Yii中最强大的部件之一。
它有一个属性名叫 dataProvider ,这个属性能够提供一个数据提供者的示例并且可以显示所提供的数据,即使用 yii\grid\GridView::columns 属性的一组列配置,在一个表格中渲染每一行数据。
例如,
use yii\grid\GridView;echo yii\grid\GridView::widget([ 'dataProvider' => $dataProvider,]);
一、表格列
表格的列是通过 GridView 配置项中的 yii\grid\GridView::columns 属性配置的.
<?phpuse yii\grid\GridView;echo GridView::widget([ 'dataProvider' => $dataProvider, //表格列值搜索功能,注意一定要配合attribute才会显示 //$searchModel = new ArticleSearch(); 'filterModel' => $searchModel, //重新定义分页样式 'layout'=> '{items}<div class="text-right tooltip-demo">{pager}</div>', 'pager'=>[ //'options'=>['class'=>'hidden']//关闭分页 'firstPageLabel'=>"First", 'prevPageLabel'=>'Prev', 'nextPageLabel'=>'Next', 'lastPageLabel'=>'Last', ] 'columns' => [ ['class' => 'yii\grid\SerialColumn'],//序列号从1自增长 // 数据提供者中所含数据所定义的简单的列 // 使用的是模型的列的数据 'id', 'username', // 更复杂的列数据 [ 'class' => 'yii\grid\DataColumn', //由于是默认类型,可以省略 'value' => function ($data) { return $data->name; // 如果是数组数据则为 $data['name'] , 例如,使用 SqlDataProvider 的情形。 }, ], ['label'=>'标题','value' => 'title'], ['label'=>'文章内容','format' => 'html','value' => 'content'], [ 'label'=>'文章类别', /*'attribute' => 'cid',产生一个a标签,点击可排序*/ 'value' => 'cate.cname' //关联表 ], [ //动作列yii\grid\ActionColumn //用于显示一些动作按钮,如每一行的更新、删除操作。 'class' => 'yii\grid\ActionColumn', 'header' => '操作', 'template' => '{delete} {update}',//只需要展示删除和更新 'headerOptions' => ['width' => '240'], 'buttons' => [ 'delete' => function($url, $model, $key){ return Html::a('<i class="fa fa-ban"></i> 删除', ['del', 'id' => $key], [ 'class' => 'btn btn-default btn-xs', 'data' => ['confirm' => '你确定要删除文章吗?',] ] ); }, ], ], ],]);?>
1. 处理时间
数据列的主要配置项是 yii\grid\DataColumn::format 属性。
它的值默认是使用 \yii\i18n\Formatter 应用组件。
[ 'label'=>'更新日期', 'format' => ['date', 'php:Y-m-d'], 'value' => 'updated_at'],//or[ //'attribute' => 'created_at', 'label'=>'更新时间', 'value'=>function($model){ return date('Y-m-d H:i:s',$model->created_at); }, 'headerOptions' => ['width' => '170'],],
2. 处理图片
[ 'label'=>'封面图', 'format'=>'raw', 'value'=>function($m){ return Html::img($m->cover, ['class' => 'img-circle', 'width' => 30] ); }],
3. 数据列有链接
[ 'attribute' => 'title', 'value' => function ($model, $key, $index, $column) { return Html::a($model->title, ['article/view', 'id' => $key]); }, 'format' => 'raw',],
4. 数据列显示枚举值(男/女)
[ 'attribute' => 'sex', 'value'=>function ($model,$key,$index,$column){ return $model->sex==1?'男':'女'; }, //在搜索条件(过滤条件)中使用下拉框来搜索 'filter' => ['1'=>'男','0'=>'女'], //or 'filter' => Html::activeDropDownList($searchModel, 'sex',['1'=>'男','0'=>'女'], ['prompt'=>'全部'] )],[ 'label'=>'产品状态', 'attribute' => 'pro_name', 'value' => function ($model) { $state = [ '0' => '未发货', '1' => '已发货', '9' => '退货,已处理', ]; return $state[$model->pro_name]; }, 'headerOptions' => ['width' => '120'] ]
阅读全文
0 0
- yii2-GridView在开发中常用的功能及技巧
- yii2-GridView在开发中常用的功能及技巧
- yii2 中GridView的三表联查的显示
- 在项目开发中Xcode的常用功能
- Yii2 GridView 中对columns的定义
- 开发中常用的功能
- GridView常用的几个技巧
- 用javascript操纵GridView中CheckBox的两个常用技巧
- 用javascript操纵GridView中CheckBox的两个常用技巧
- 用javascript操纵GridView中CheckBox的两个常用技巧
- 用javascript操纵GridView中CheckBox的两个常用技巧
- yii2常用小技巧
- Yii2.0中在GridView添加字体图标按钮
- 在VS2005中GridView导入Excel的两点小技巧
- 在VS2005中GridView导入Excel的两点小技巧
- 在VS2005中 GridView导入Excel的两点小技巧
- 在VS2005中 GridView导入Excel的两点小技巧
- ASP.NET中GridView控件常用技巧
- Eclipse导出war包时去掉SVN文件
- 23种设计模式(20)-状态模式
- C常用自定义函数集锦
- 详解python中的__new__方法
- SpringData中的PagingAndSortingRepository接口
- yii2-GridView在开发中常用的功能及技巧
- Linux学习笔记(7)
- C/C++中输入多组数据方法
- 使用数据库UNIQUE constraint failed
- Spring MVC与Struts2的区别
- 结合FlexboxLayout实现标签选择功能
- ionic3(一)安装和配置
- php判断密码强度
- js 中处理金额数据 四舍五入数据(,处理和保留小数处理)