如何获取CheckboxColumn Gridview - Yii2中选定的数据/项目行 [英] How can I get the selected data/item rows in CheckboxColumn Gridview - Yii2

查看:308
本文介绍了如何获取CheckboxColumn Gridview - Yii2中选定的数据/项目行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我只能得到一个网格中的 使用此代码:

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

需要一些关于如何获取网格中所有值的建议。 ..



以下是我的代码片段控制器/视图:
$ b

Controller:

  public function actionBulk(){
$ action = Yii :: $ app - >要求─>柱( '动作');
$ selection =(array)Yii :: $ app-> request-> post('selection');
print_r($ selection);
}

查看: b

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

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

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

以下是我的附件:







如何返回item_id和description?

'checkboxOptions'=> ,您可以删除它吗?

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

<?= Html :: dropDownList('action','',[''=>'选中标记为:','c'=>'已确认','nc' =>'无确认'],['class'=>'dropdown',])?>>

<?= Html :: submitButton('Send',['class'=>'btn btn-info',]);?>

<?= GridView :: widget([
'dataProvider'=> $ dataProvider,
'columns'=> [
['class' =>'yii\grid\CheckboxColumn'],
...
],
]); ?>

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

在控制器中:

  public function actionBulk(){
$ action = Yii :: $ app-> request-> post('action');
$ selection =(array)Yii :: $ app-> request-> post('selection'); //类型转换
foreach($ selection as $ id){
$ model = Post :: findOne((int)$ id); //做一个类型转换
//做你的东西
$ model-> save();
//或删除
}
}


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?

解决方案

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

这篇关于如何获取CheckboxColumn Gridview - Yii2中选定的数据/项目行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆