【问题标题】:How can I get the selected data/item rows in CheckboxColumn Gridview - Yii2如何在 CheckboxColumn Gridview 中获取选定的数据/项目行 - Yii2
【发布时间】:2016-08-11 10:18:11
【问题描述】:

我在使用 checkboxColumn 获取所有选定的值/数据 Yii2 Gridview 时遇到问题。

我只能使用以下代码在 grid 中获得 一个

         'class' => 'yii\grid\CheckboxColumn',
         'checkboxOptions' => function($model, $key, $index, $widget) {
            return ['value' => $model['item_id'] ];
         },

需要一些关于如何获取网格中所有值的建议...

这是我的代码代码 sn-p 控制器/视图:

控制器:

public function actionBulk(){
   $action=Yii::$app->request->post('action');
   $selection=(array)Yii::$app->request->post('selection');
   print_r($selection);
}

查看:

<?=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();?> 

这是我的附件:

This is the GridView

This is the Result in the GridView (Selected Data only returns item_id)

如何同时返回 item_id 和 description?

【问题讨论】:

    标签: php gridview yii2 yii2-advanced-app


    【解决方案1】:

    你的代码'checkboxOptions' =&gt;有问题,你能把它去掉吗?

    <?=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();?> 
    

    在控制器中:

     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
          }
        }
    

    【讨论】:

    • tnx @muhammad-shahzad
    【解决方案2】:

    基本上,我使用的是 yii 的 CheckboxColumn:

    <?php
        namespace common\grid;
    
        class CheckboxColumn extends \yii\grid\CheckboxColumn {
    
            public $headerOptions  = ['class' => 'text-center', 'style' => 'width: 5em'];
            public $contentOptions = ['class' => 'text-center'];
    
        }
    ?>
    

    然后我编写了一个 jquery 插件,用于触发对选定项目的操作,以及自定义操作等,这里是相关的 javascript 代码,其中options.grid 是您网格的 id/选择器,例如'#grid':

        var selection = [];
        $(options.grid + ' input:checkbox[name="selection[]"]:checked').each(function() {
            selection.push($(this).val());
        });
    

    所以var selection 包含一个带有我的项目ID 的数组。长度是:

    selection.length
    

    【讨论】:

    • tnx 回复。但我想我要从网格中查询选定的ID...
    • 您可以将gridview的id作为参数传递。然后使用这个#id 而不是“options.grid”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 2017-05-18
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    相关资源
    最近更新 更多