如何在 CheckboxColumn Gridview - Yii2 中获取选定的数据/项目行

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

问题描述

我在使用 checkboxColumn 获取所有选定的值/数据 Yii2 Gridview 时遇到问题.

我只能使用此代码在网格中获得一个:

 'class' =>'yiigridCheckboxColumn','checkboxOptions' =>函数($model,$key,$index,$widget){返回 ['值' =>$model['item_id']];},

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

这是我的代码片段控制器/视图:

控制器:

公共函数actionBulk(){$action=Yii::$app->request->post('action');$selection=(array)Yii::$app->request->post('selection');打印_r($选择);}

查看:

<?=Html::beginForm(['transjournal/bulk'],'post');?><?=GridView::widget(['数据提供者' =>$数据提供者,'有边界'=>真,'条纹'=>真,'浓缩'=>真,'悬停'=>真,'出口' =>错误的,'showOnEmpty' =>错误的,'面板'=>['after'=>Html::submitButton('<i class="glyphicon glyphicon-plus"></i> Posted', ['class' => 'btn btn-success']),],'列' =>[['类' =>'yiigridCheckboxColumn','checkboxOptions' =>函数($model,$key,$index,$widget){返回 ['值' =>$model['item_id']];},],'item_id','描述',],]);?><?= Html::endForm();?>

这是我的附件:

如何同时返回 item_id 和 description?

解决方案

您的代码有问题 'checkboxOptions' =>,您可以删除它吗?

<?=Html::submitButton('Send', ['class' => 'btn btn-info',]);?><?=GridView::widget(['数据提供者' =>$数据提供者,'列' =>[['类' =>'yiigridCheckboxColumn'],...],]);?><?= Html::endForm();?>

在控制器中:

 公共函数 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' => 'yiigridCheckboxColumn',
         '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' => 'yiigridCheckboxColumn',
        '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' => 'yiigridCheckboxColumn'],
        ...
     ],
  ]); ?>

<?= 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 中获取选定的数据/项目行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:GridView 行作为链接,Yii2 中的操作列项除外 下一篇:使用图形时无法从数据库中获取数据到脚本标签

相关文章

最新文章