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

爱⌒轻易说出口 提交于 2019-12-24 02:52:25

问题


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

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

How can I return both item_id and description?


回答1:


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
      }
    }



回答2:


basically, i am using yii's CheckboxColumn:

<?php
    namespace common\grid;

    class CheckboxColumn extends \yii\grid\CheckboxColumn {

        public $headerOptions  = ['class' => 'text-center', 'style' => 'width: 5em'];
        public $contentOptions = ['class' => 'text-center'];

    }
?>

then i wrote a jquery plugin for triggering operations with selected items, plus custom Actions and so on, here the relevant javascript code, where options.grid is the id/selector for your grid, e.g. '#grid':

    var selection = [];
    $(options.grid + ' input:checkbox[name="selection[]"]:checked').each(function() {
        selection.push($(this).val());
    });

so var selection contains an array with my item ids. length is:

selection.length


来源:https://stackoverflow.com/questions/38893566/how-can-i-get-the-selected-data-item-rows-in-checkboxcolumn-gridview-yii2

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