How to let CheckboxColumn do its jobs

Hi guys, I use CheckBoxColumn like this in GridView




 [

'class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($data) {

 return ['value' => $data->id];

 },

 ],



My intention is to get deleted all rows of table,where checkbox is activated. Unfortunately, I didn’t find further informations about this. All i found is like this




Users may click on the checkboxes to select rows of the grid. The selected rows may be obtained by calling the following JavaScript code:

var keys = $('#grid').yiiGridView('getSelectedRows');



this won’t help me achieving my intention. So, I ask U,which components(MCV) are involved. Controller and View,as I assume -

But how to get started?

Arbeiten Sie bei der TK?

In yourController/yourAction get the selected rows using Yii::$app->request->post(‘selection’)




<?= Html::beginForm(['yourController/yourAction'], 'post'); ?>


    <?= GridView::widget([

            'dataProvider' => $dataProvider,

            'columns' => [

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

              

                  ...

            ],

        ]);

    ?>


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


<?= Html::endForm(); ?>




class YourController extends Controller {


    public function yourAction(){

        

        /* get the selected rows */

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

 			

        foreach ($selection as $item) {

            /* your code to do with the checked rows*/

   	}

}

Nein, ich weiss nicht einmal,wass die TK ist- die Techniker Krankenkasse?

Where is context between post(‘selection’) and checkboxes?

Records only have to get deleted after having pushed submittButton. SubmitButton is defined like this




<p><?= Html::a(Yii::t('app', 'Datensätze löschen'), ['/mail-eingang/delete_records'], ['class' => 'btn btn-danger']) ?>



[color="#1C2837"][size="2"]

[/size][/color]

[color="#1c2837"][size="2"]There is no explict context and it has been made by the Framework based on the function [/size][size="2"]$_POST[/size][/color][color="#111111"] of [/color][color="#1C2837"][size="2"].PHP.[/size][/color]

[color="#1C2837"][size="2"]

[/size][/color]

this should work:




<?= Html::beginForm(['/mail-eingang/delete_records'], 'post'); ?>


    <?= GridView::widget([

            'dataProvider' => $dataProvider,

            'columns' => [

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

              

                  ...

            ],

        ]);

    ?>


    <?= Html::submitButton(Yii::t('app', 'Datensätze löschen'), ['class' => 'btn btn-danger']); ?>


<?= Html::endForm(); ?>




class Mail-eingangController extends Controller {


    public function delete_records(){

        

        /* get the selected rows */

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

    		

        foreach ($selection as $item) {

            /* your code to do with the checked rows*/

   	}

}

No, as I assumed, it won`t work. I redefined CheckboxColumn, as U suggested:




.

.


<?=Html::beginForm(['/mail-eingang/delete_records'], 'post');

.

.

<?= Html::submitButton(Yii::t('app', 'Datensätze löschen'), ['class' => 'btn btn-danger']); ?>

.

.

$gridColumn[

.

.

];


 $gridColumn_ausgang = [

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

.

.

];

.

.

<?=

GridView::widget([

'dataProvider' => $dataProvider_ausgang,

'filterModel' => $searchModel_ausgang,

'columns' => $gridColumn_ausgang,

.

.


<?=

GridView::widget([

'dataProvider' => $dataProvider,

'filterModel' => $searchModel,

'columns' => $gridColumn,

.

.

And in Controller var_dump($selection) will show empty array,also after having selected all checkboxes. So,there is no context





    public function actionDelete_records() {

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

        var_dump($selection); //this will show empty array


        foreach ($selection as $item) {

     		var_dump($item); //this will show nothing

        }

    }



Running this code shows up,that selection is in no context with my checkbox





        if (null==((array)Yii::$app->request->post('selection'))) {

            echo'There is not context between selection and ur checkBoxes...';

        }      



Ur solution will work. But it works only for one GridView. But I use two GridViews. Implementing two GridViewsl like this will prevent Controller doing its job





    public function actionIndex() {

        $searchModel = new MailEingangSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $searchModel_ausgang = new MailAusgangSearch();

        $dataProvider_ausgang = $searchModel_ausgang->search(Yii::$app->request->queryParams);


        return $this->render('index', [

                    'searchModel' => $searchModel,

                    'searchModel_ausgang' => $searchModel_ausgang,

                    'dataProvider' => $dataProvider,

                    'dataProvider_ausgang' => $dataProvider_ausgang

        ]);

    }