如何获取CheckboxColumn Gridview - Yii2中选定的数据/项目行 [英] How can I get the selected data/item rows in 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?
<?= 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 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屋!