CoD_Arkalodia_3 CoD_Arkalodia_3 - 3 years ago 475
PHP Question

How can I get the selected data/item rows in CheckboxColumn Gridview - Yii2

I have a problem on getting all the selected values/data Yii2 Gridview using checkboxColumn.

I can only get one of the value in the grid using this code:

'class' => 'yii\grid\CheckboxColumn',
'checkboxOptions' => function($model, $key, $index, $widget) {
return ['value' => $model['item_id'] ];
},


Need some suggestions on how can I get all the values in the grid...

Here is my Code Code snippet Controller/View:

Controller:

public function actionBulk(){
$action=Yii::$app->request->post('action');
$selection=(array)Yii::$app->request->post('selection');
print_r($selection);
}


View:

<?=Html::beginForm(['transjournal/bulk'],'post');?>

<?=GridView::widget([
'dataProvider' => $dataProvider,
'bordered'=>true,
'striped'=>true,
'condensed'=>true,
'hover'=>true,
'export' => false,
'showOnEmpty' => false,
'panel'=>[
'after'=>Html::submitButton('<i class="glyphicon glyphicon-plus"></i> Posted', ['class' => 'btn btn-success']),
],
'columns' => [
[
'class' => 'yii\grid\CheckboxColumn',
'checkboxOptions' => function($model, $key, $index, $widget) {
return ['value' => $model['item_id'] ];
},
],
'item_id',
'description',
],
]);
?>

<?= Html::endForm();?>


Here is my attachment:

This is the GridView

enter image description here

This is the Result in the GridView (Selected Data only returns item_id)

enter image description here

How can I return both item_id and description?

Answer Source

Issue with your code 'checkboxOptions' =>, can you remove it?

<?=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'],
        ...
     ],
  ]); ?>

<?= Html::endForm();?> 

In Controller:

 public function actionBulk(){
       $action=Yii::$app->request->post('action');
       $selection=(array)Yii::$app->request->post('selection');//typecasting
       foreach($selection as $id){
        $model = Post::findOne((int)$id);//make a typecasting
        //do your stuff
        $model->save();
        // or delete
      }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download