1

this is my gridview. I want to send key and textinput to controller for send value again to another gridview in same page.

'columns' => [
    [
        'class' => 'yii\grid\CheckboxColumn',
        'checkboxOptions' => function($model) {
            return ['value' => $model->m_id, 'data' => ['key' => $model->m_id]];
        }
    ],
    'm_name',
    [
        'label' => 'จำนวน',
        'value' => function(){
            return Html::textInput('od_amount', null, ['id' => 'od_amount']);

        },
        'format' => 'raw'
    ],
    <?php
    $this->registerJs('
        $("#btn-add-group").click(function(){
            var id_cases = $("#gridview-id").yiiGridView("getSelectedRows"); 
    ?>
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118

2 Answers2

0

getSelectedRows just returns the keys. You could try getting all the "checked" checkboxes in the grid container as follows:

$("#gridview-id").find('[type="checkbox"]:checked').each(function(){ console.log(this); })

From this you should be able to use $(this).data('key') and $(this).attr('value') in place of the console.log.

This would assumes that you only have one checkbox column but if you have more you could add a class to one checkbox column and then use that in the jQuery selector.

rob006
  • 21,383
  • 5
  • 53
  • 74
ChrisB
  • 611
  • 11
  • 24
0

If you want to get the keys in the controller

Example:

<?=Html::beginForm(['Gridselected'],'post');?>    
<?= GridView::widget([
    'id' => 'griditems',
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',

      [
        'label' => 'จำนวน',
        'value' => function ($model, $key, $index) {
            return Html::textInput("od_amount[$key]", null, ['id' => "od_amount[$key]"]);
        },
        'format' => 'raw'
      ],

       //...
        ['class' => 'yii\grid\CheckboxColumn'],
    ],

]); ?>

<?=Html::submitButton('Send', ['class' => 'btn btn-primary']);?>
<?= Html::endForm();?>

In controller:

public function actionGridselected(){
  if ( Yii::$app->request->post() ) {

    $select = (array)Yii::$app->request->post('selection'); //checkbox (array)
    $odAmount=(array)Yii::$app->request->post('od_amount');


    $Provider = new \yii\data\ActiveDataProvider([
    'query' => yourModel::find()->->where(['id' => $select]),
    ]);

   // for Example: Print all the amount of inputs that are checked (in checkboxcolumn)
                foreach ($odAmount as $key => $value) {
                     if (in_array($key, $arr_select)){
                        $od_value[] = $value;
                    }
                }
                print_r($od_value);
  }
}
rob006
  • 21,383
  • 5
  • 53
  • 74
user206
  • 1,085
  • 1
  • 10
  • 15
  • I have tried your way but it's not working can you please see my [question](https://stackoverflow.com/questions/56290355/yii2-working-with-multiple-check-boxes-in-gridview)? – Moeez May 25 '19 at 04:16