How can I get the selected data/item rows in CheckboxColumn Gridview - Yii2(如何在 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::beginForm(['controller/bulk'],'post');?><?=Html::dropDownList('action','',[''=>'标记选择为:','c'='=>'Confirmed','nc'=>'No Confirmed'],['class'=''dropdown',])?><?=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 中获取选定的数据/项目行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 CheckboxColumn Gridview - Yii2 中获取选定的数据
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01