我如何处理Yii2 gridview中的复选框列? [英] How I can process a checkbox column from Yii2 gridview?

查看:747
本文介绍了我如何处理Yii2 gridview中的复选框列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的_add.php代码:

$ b $

我需要在GridView窗口小部件视图中从一组复选框中将行插入到表中。 b

  $ itemsQuery = Inventory :: find(); 
$ itemsQuery-> andFilterWhere(['inv_status'=> 1,'inv_condition'=> 2]);
$ dataProvider = new ActiveDataProvider([
'query'=> $ itemsQuery,
]);
echo GridView :: widget([
'id'=>'griditems',
'dataProvider'=> $ dataProvider,$ b $'columns'=> [$ b $'['attribute'=>'inv_group','value'=>'invGroup.inv_group'],
['attribute'=>'inv_class','value'=>'invClass .inv_class'],
'brand',
'model',
'description',
['class'=&';'yii\grid\CheckboxColumn'] ,
],
]);

我尝试过使用JavaScript,但是无法获得结果:

$ b $(b)

  $('element')。one('click',function(){
var keys = $('#griditems')。 yiiGridView('getSelectedRows');
$ .post({
url:'pick-items / processselected',// your controller action
dataType:'json',
data :{keyylist:keys},
success:function(data){
if(data.status ==='success'){
alert('Total price is'+ data.total );
}
},
});
});

我已经尝试过几种可能的解决方案,但没有结果,我读过文档,博客,论坛和我不能这样做。



如何从gridview表格中的一组复选框将数据处理到控制器中?

javascript



在你的 GridView 中,只需改变 ['class'=> 'yii\grid\CheckboxColumn'] show below code

  $ itemsQuery = Inventory :: find(); 
$ itemsQuery-> andFilterWhere(['inv_status'=> 1,'inv_condition'=> 2]);
$ dataProvider = new ActiveDataProvider([
'query'=> $ itemsQuery,
]);
echo GridView :: widget([
'id'=>'griditems',
'dataProvider'=> $ dataProvider,$ b $'columns'=> [
[
'属性'=>'inv_group',
'值'=>'invGroup.inv_group'
],
[
'属性'=>'inv_class',
'value'=>'invClass.inv_class'
],
'brand',
'model',
'描述',
[
'class'=>'yii\grid\CheckboxColumn','checkboxOptions'=>函数($ model){
return ['value'= > $ model-> Your_unique_id];
},
],
],
]);

现在访问复选框控制器 。在您的控制器中添加以下代码以访问所选行

  isset($ _ REQUEST ['selection'])


I need to insert rows into a table from a set of checkboxes in a gridview widget view.

My _add.php code:

$itemsQuery = Inventory::find();
$itemsQuery->andFilterWhere(['inv_status' => 1, 'inv_condition' => 2]);
$dataProvider = new ActiveDataProvider([
                                       'query' => $itemsQuery,
                                       ]);
echo GridView::widget([
    'id' => 'griditems',
    'dataProvider' => $dataProvider,
    'columns' => [
        ['attribute' => 'inv_group', 'value' => 'invGroup.inv_group'],
        ['attribute' => 'inv_class', 'value' => 'invClass.inv_class'],
        'brand',
        'model',
        'description',
        ['class' => 'yii\grid\CheckboxColumn'],
    ],
]);

I've tried using JavaScript, however I can't get results:

$('element').one('click',function() {
var keys = $('#griditems').yiiGridView('getSelectedRows');
$.post({
   url: 'picked-items/processselected', // your controller action
   dataType: 'json',
   data: {keylist: keys},
   success: function(data) {
      if (data.status === 'success') {
          alert('Total price is ' + data.total);
      }
   },
});
});

I've tried several possible solutions with no results, I've read documentations, blogs, forums and I can't do it.

How can I process data into my controller from a set of checkboxes in a gridview table?

解决方案

You can not need to use javascript

In your GridView just change ['class' => 'yii\grid\CheckboxColumn'] show below code

My _add.php

$itemsQuery = Inventory::find();
$itemsQuery->andFilterWhere(['inv_status' => 1, 'inv_condition' => 2]);
$dataProvider = new ActiveDataProvider([
                                   'query' => $itemsQuery,
                                   ]);
echo GridView::widget([
   'id' => 'griditems',
   'dataProvider' => $dataProvider,
   'columns' => [
      [
         'attribute' => 'inv_group', 
         'value' => 'invGroup.inv_group'
      ],
      [
          'attribute' => 'inv_class', 
          'value' => 'invClass.inv_class'
      ],
      'brand',
      'model',
      'description',
      [
          'class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model) {
                return ['value' => $model->Your_unique_id];
            },
      ],
   ],
]);

Now access the checkbox selected data in controller. Add below code in your controller to access selected row

isset($_REQUEST['selection'])

这篇关于我如何处理Yii2 gridview中的复选框列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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