問題描述
我在使用 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 中獲取選定的數據/項目行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!