yii2GridView的简单使用

初学Yii2.0,用到GridView,翻阅官方文档,也没怎么看明白,自己慢慢摸索出来的,仅供参考


			GridView::widget([
			    'dataProvider' => $dataProvider,// 你传过来的ActiveDataProvider
			    // 'filterModel' => $searchModel,
			    'columns' => [
			        ['class' => 'yii\grid\SerialColumn'],// 第一列排序

			        'sid',// 第二列,sid,与你查询的model字段相对应,可以少,不可以多
			        [
			            'attribute' => 'sname',
			            'label'=>'姓名',// 自定义列名
			        ],// 第三列,sname

			        [
			        	'class' => 'yii\grid\ActionColumn',// 动作列,默认三个动作,分别为{view},{update},{delete}
			        	'header' => '操作',// 列名
			        	'template' => '{stuent-view} {studnet-update} {student-delete}',// 定义这一列里面有几个操作,这里为查看,更新,删除
			        	'buttons' => [// 为你template中声明的操作声明动作
			        		'stuent-view' => function ($url, $models, $key) {// 对应{student-view},三个参数,最主要的$key,为你model主键的id
							$url = ['student/view', 'id'=>$key];// 为下面a链接的url,此处指向StudentController的actionView方法
			        			$options = [
						            'title' => '查看',
						            'aria-label' => '查看',
						            'data-pjax' => '0',
						        ];
						        return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
			        		},
			        		'studnet-update' => function ($url, $models, $key) {// 对应{student-update}
			        			$url = ['student/update', 'id'=>$key];
			        			$options = [
						            'title' => '更新',
						            'aria-label' => '更新',
						            'data-pjax' => '0',
						        ];
						        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
			        		},
			        		'student-delete' => function ($url, $models, $key) {// 对应{student-delete}
			        			$url = ['student/delete', 'id'=>$key];
			        			$options = [
						            'title' => '删除',
						            'aria-label' => '删除',
						            'data-pjax' => '0',
							    'data-method' => 'post'
						        ];
						        return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
			        		}
			        	]
			        ],// 操作
			    ],
			]);




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值