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); } ] ],// 操作 ], ]);
?