整理文档,发现一个用GridView实现数据全选及批量删除按钮示例,稍微整理精简一下做下分享。
先来看实现的效果
关键代码如下:
PHP;">
= GridView::widget([
'dataProvider' => $dataProvider,'showFooter' => true,//设置显示最下面的footer
'id' => 'grid','columns' => [
[
'class'=>CheckBoxColumn::className(),'name'=>'id','headerOptions' => ['width'=>'30'],'footer' => '','footerOptions' => ['colspan' => 5],],['attribute' => 'id','footerOptions' => ['class'=>'hide']],//其他列每个都要增加footerOptions项,设置class为hide,到达隐藏效果;
['attribute' => 'username',[
'attribute' => 'status','value' => function($model){
if ($model->status == Admin::STATUS_ACTIVE){
return '启用';
}
return '禁用';
},'footerOptions' => ['class'=>'hide']
],['class' => 'yii\grid\ActionColumn','header' => '管理操作','layout' => "{items}\n{pager}"
]); ?>
最后已选数据的获取:
PHP;">
var ids = $("#grid").yiiGridView("getSelectedRows");
获取数据后就可以ajax提交到想要的controller