Skip to content Skip to sidebar Skip to footer

Yii2-working With Multiple Check Boxes In Gridview

I have a gridview in which there also a checkbox. 'btn btn-success', 'id'=>'dco']) ?>

Solution 1:

Because you left a comment here, I used the beginForm method:

in View

<divclass="pre-scrollable"><?php Pjax::begin() ?><?=Html::beginForm(['test'],'post');?><?= Html::submitButton('Disconnect', ['class' => 'btn btn-success', 'name'=>'dco', 'value'=>'dco', 'id'=>'dco','style'=>'margin:0 10px;']) ?><?= Html::submitButton('Connect', ['class' => 'btn btn-primary', 'name'=>'rco', 'value'=>'rco','id'=>'rco']) ?><?= GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
    'columns' => [

        ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($d) {
            return ['value' => $d['msn']];
        }],

        'customer_id',       
        'dept_name:ntext',
        'sub_div_name',
        'division_name',

        'allowed_units',
        'msn',

        'units_consumed',
        [
            'label' => 'Disconnected',
            'attribute' => 'disconnected',
            'format'=>'raw',
            'contentOptions' => ['style'=>'text-align:center'],
            'value' => function($model){
                return$model->disconnected == 1 ? '<span class="glyphicon glyphicon-ok text-success"></span>' : '<span class="glyphicon glyphicon-remove text-danger"></span>';
            },
            'filter' => Html::activeDropDownList($searchModel, 'disconnected', [''=>'All','1'=>'Yes','0'=>'No'], ['class' => 'form-control']),
        ],

        'active_energy_total_m',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?><?= Html::endForm();?><?php Pjax::end() ?></div>

in controllers

publicfunctionactionTest()
{
    $searchModel  = \Yii::createObject(\app\models\TestSearch::className());
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

      $arr_select=(array)Yii::$app->request->post('selection');  //An array of selected items (checkbox)$dco=Yii::$app->request->post('dco');  // submitButton Disconnect$rco=Yii::$app->request->post('rco');  // submitButton Connectif ($dco === 'dco') {
           //code for Disconnect here ($query)// example:  $modelTest::updateAll(['disconnected' => 1], ['msn' => $arr_select]);
      } elseif ($rco == 'rco') {
           //code for Connect here ($query)
      }

    }

    return$this->render('test', [
        'dataProvider' => $dataProvider,
        'searchModel'  => $searchModel,
    ]);
}

You can optimize your code in the controller. Of course, I think if you use the following method, it is better (a button and ...). You can easily decide on the controller.

<?= Html::dropDownList('action','',[ 'Connect' =>'Connect','Disconnect' =>'Disconnect'],['prompt' => 'Please select','class'=> 'field-black input-sm']) ?><?= Html::submitButton('Apply', ['class' => 'btn btn-success','style'=>'margin:0 10px;']) ?>

You can place the above code instead of the two submitButton. Then decide on the controller according to the dropDownList value. Like below: also add:, 'data-pjax'=>''

$_action=Yii::$app->request->post('action'); // dropDown$arr_select=(array)Yii::$app->request->post('selection'); //selected items 

Post a Comment for "Yii2-working With Multiple Check Boxes In Gridview"