yii2/grid/CheckboxColumn getting checked boxes

I have a standard grid index/view with “GridView::widget” and “yii\grid\CheckboxColumn”. I have been reading documentation on how to get the checked row ids. The documentation says to add javascript but on examining “yii.gridView.js” it appears that is already included. I don’t see the keys getting in $_POST. I feel like I may be missing a number of steps. Can someone please walk me through the steps? Much appreciated.

What’s your form config and controller action handling it?

My controller code is:

public function actionSelected()
{
$keys = "Nothing";
if (isset($_POST['keylist'])) {
    $keys = "Something";
    $keys = \yii\helpers\Json::decode($_POST['keylist']);
    }
    //return $this->redirect(['selected']);
    return $this->render('selected',['keys' => $keys]);
}

My index view code

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => 
        ['class' => 'yii\grid\CheckboxColumn'],

… etc.

My selected view code

<p>
    <?= $keys ?>
</p>

For now all it get is “Nothing” in the selected view output

I have tried various options with yii\grid\CheckboxColumn but no luck.

Not sure what form config I need to supply. Just a index grid view that will return/display the array of IDs selected in the view (for now).

Thanks so much

How do you handle submit? Displaying Data: Data Widgets | The Definitive Guide to Yii 2.0 | Yii PHP Framework

I am beginning to see where the issue may be, although not sure how to resolve it. Below is the submit button code (in the index/grid view file) - which is not a form and just a link. I have a grid in an index view and don’t have a submit form, but I can see I may need it to do a POST.

<p>
    <?= Html::a('With Selected', ['selected'], ['class' => 'btn btn-success']) ?>
</p>

Thanks for your prompt responses.

Yes. You need a JavaScript handler that is getting a list of values and sends a request.

If it’s not too much to ask, can you provide one working / reproducible example? My knowledge of JavaScript is very close to non-existent. :pleading_face:

Sorry, can’t do it fast. Maybe someone else can…

@snathan The example

1 Like

Thank you Skull. I actually used user206’s answer of using forms and no javascript. I was going to post my version of it, but you beat me to it. Appreciate you finding this for me.

2 Likes