yii2.0Gridview的簡單使用說明

 主要針對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);
			        		}
			        	]
			        ],// 操作
			    ],
			]);

轉自:https://blog.csdn.net/zlh13854157321/article/details/52035629

經過本人驗證,感覺對於yii2.0新手來說幫助很大。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章