整理文档,发现一个用gridview实现数据全选及批量删除按钮示例,稍微整理精简一下做下分享。
先来看实现的效果
关键代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?= gridview::widget([ 'dataprovider' => $dataprovider , 'showfooter' => true, //设置显示最下面的footer 'id' => 'grid' , 'columns' => [ [ 'class' =>checkboxcolumn::classname(), 'name' => 'id' , 'headeroptions' => [ 'width' => '30' ], 'footer' => '<button href="#" rel="external nofollow" class="btn btn-default btn-xs btn-delete" url="' . url::toroute( 'admin/delete' ) . '">删除</button>' , 'footeroptions' => [ 'colspan' => 5], ], [ 'attribute' => 'id' , 'footeroptions' => [ 'class' => 'hide' ]], //其他列每个都要增加footeroptions项,设置class为hide,到达隐藏效果; [ 'attribute' => 'username' , 'footeroptions' => [ 'class' => 'hide' ]], [ 'attribute' => 'status' , 'value' => function ( $model ){ if ( $model ->status == admin::status_active){ return '启用' ; } return '禁用' ; }, 'footeroptions' => [ 'class' => 'hide' ] ], [ 'class' => 'yii\grid\actioncolumn' , 'header' => '管理操作' , 'footeroptions' => [ 'class' => 'hide' ]], ], 'layout' => "{items}\n{pager}" ]); ?> |
最后已选数据的获取:
1
|
var ids = $( "#grid" ).yiigridview( "getselectedrows" ); |
获取数据后就可以ajax提交到想要的controller
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.yiichina.com/tutorial/1178