Yii2 Gridview bulk action using checkbox column. How to properly create checkbox column in gridview for bulk actions

To add a CheckboxColumn to the yiigridGridView, add it to the columns configuration as follows:
'columns' => [
    
// ...
    
[
        
'class' => 'yiigridCheckboxColumn',
        
// you may configure additional properties here
    
],
]
This is the view:
<?=Html::beginForm(['controller/bulk'],'post');?>
<?=Html::dropDownList('action','',[''=>'Mark selected as: ','c'=>'Confirmed','nc'=>'No Confirmed'],['class'=>'dropdown',])?>
<?=Html::submitButton('Send', ['class' => 'btn btn-info',]);?>
<?=GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yiigridCheckboxColumn'],
'id',
],
]); ?>
<?= Html::endForm();?>
This is the controller:
public function actionBulk(){
$action
=Yii::$app->request->post('action');
$selection
=(array)Yii::$app->request->post('selection');//typecasting
foreach($selection as $id){
$e
=Evento::findOne((int)$id);//make a typecasting
//do your stuff
$e
->save();
}
}

1 comment

Leave a comment

Your email address will not be published. Required fields are marked *