Home >Backend Development >PHP Tutorial >Yii2 uses GridView to implement buttons for selecting all data and deleting data in batches
This article mainly introduces the example of using GridView to implement all data selection and batch deletion buttons in yii2. It has certain reference value. Those who are interested can learn more.
After sorting through the documents, I found an example of using GridView to implement all-data selection and batch deletion buttons. I sorted it out a bit and simplified it for sharing.
Let’s first look at the effect of implementation
The key code is as follows:
<?= 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}" ]); ?>
Getting the final selected data:
var ids = $("#grid").yiiGridView("getSelectedRows");
After getting the data, you can ajax submit it to the desired controller
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
yii2 implements paging and paging with search functions
About how to write search paging jQuery in the YII framework
The above is the detailed content of Yii2 uses GridView to implement buttons for selecting all data and deleting data in batches. For more information, please follow other related articles on the PHP Chinese website!