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

问题描述

我需要从 gridview 小部件视图中的一组复选框中将行插入表中.

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

我的 _add.php 代码:

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' => 'yiigridCheckboxColumn'],
    ],
]);

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

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.

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

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

推荐答案

你可以不需要使用 javascript

在您的 GridView 中,只需更改 ['class' =>'yiigridCheckboxColumn'] 显示下面的代码

In your GridView just change ['class' => 'yiigridCheckboxColumn'] show below code

我的 _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' => 'yiigridCheckboxColumn', 'checkboxOptions' => function($model) {
                return ['value' => $model->Your_unique_id];
            },
      ],
   ],
]);

现在访问 controller 中的复选框选中数据.在控制器中添加以下代码以访问选定的行

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

Yii::$app->request->post('selection');

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

09-05 23:48