Gridview 网格小部件
一、特点:
1、是yii中功能最强大的小部件之一;
2、非常适合快速建立系统的管理后台。
3、用 dataProvider 键来指定数据的提供者
4、用 filterModel 键指定一个能够提供搜索过滤功能的搜索模型类
5、用columns 键指定需要展示的键及其格式
<?= GridView::widget([ 'dataProvider' => $dataProvider, // dateProvider 键数据提供者 'filterModel' => $searchModel, // filterModel 键指定一个能够提供搜索过滤功能的搜索模型类 'columns' => [ // columns 键指定需要展示的键及其格式 ['class' => 'yiigridSerialColumn'], // 序列号:行号,从 1 开始自动增长 // 数据列:用于显示数据 [ 'attribute' => 'id', 'contentOptions'=> ['width'=>'30px'], // 设定数据列 html 属性 ], 'title', // 简写 [ 'attribute' => 'author_id', 'value' => 'author.nickname', // 值 ], 'tags:ntext', [ 'attribute' => 'status', 'value' => 'status0.name', 'filter' => commonmodelsPoststatus::find() ->select(['name','id']) ->orderBy('position') ->indexBy('id') // 将 id 作为数组的键 ->column(), // filter 自定义过滤条件的输入框 ], [ 'attribute' => 'update_time', 'format' => ['date','php:Y-m-d H:i:s'], // 相当于调用 php 的 date 函数 ], ['class' => 'yiigridActionColumn'], // 动作列 :显示动作按钮,例如:查看、更新、删除等 // 复选框列:显示一个复选框 ], ]); ?> 上面的 title 为简写格式,完整的写法如下: [ 'class' => DataColumn::className(), // 类名 'attribute' => 'title', // 指定需要展示的属性 'format' => 'text', // 格式 'label' => '标题', // 标签名 ],
注:本文为作者(44106-kangaroo) 看完魏羲教你学Yii2.0 视频后所记,如有转载请注明出处:http://www.cnblogs.com/chrdai/p/7966330.html