Yii2 How to properly create checkbox column in gridview for bulk actions?

那年仲夏 提交于 2019-11-29 04:36:42

I solved it myself like this:

This way the form gets protected from CSRF and everything goes in a POST request.

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' => 'yii\grid\CheckboxColumn'],
'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();
    }
    }
Mihai P.

Well first of all do not do what you do. Like you said that is opened to CRSF. Use POST and not GET to take the values to your controller. arogachev's link and answer might tells you how to do that I believe. 1 additional thing with the CRSF, you can get the CRSF of the page from yii, take a look here Getting bad request (#400) on Ajax calls using Yii 2 but not on the accepted answer (that is mine :)) but to the other one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!