Bulk actions made easy...

I’ve searched for a couple of days now and found only a few topics about bulk actions.

I want to perform some actions to selected rows in my table.

I think i found a nice easy solution but want to now if it’s ok like this.

I would also like to know how to POST the data instead of using GET.

the code:




view/index.php:


<?php


use yii\helpers\Html;

use yii\grid\GridView;

use yii\web\View;

use yii\widgets\ActiveForm;

use yii\grid\CheckboxColumn;

//use kartik\grid\GridView;

use yii\helpers\Url;


/* @var $this yii\web\View */

/* @var $searchModel backend\modules\kruidvat\models\KruidvatFilesSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */


$this->title = 'Kruidvat Files';

$this->params['breadcrumbs'][] = $this->title;

?>


<?php \yii\widgets\Pjax::begin(); ?>

<div class="kruidvat-files-index">


 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>


    <h1><?= Html::encode($this->title) ?></h1>

   

    <?php //echo $this->render('_search', ['model' => $searchModel]); ?>


    <?= Html::submitButton('Button 3', [ 'class' => 'btn btn-success' ]); ?>

    <?= Html::button('Button 2', [ 'name' => 'button2', 'id' => 'button2' , 'class' => 'btn btn-primary']); ?>


   <? if (isset($pk)) print_r($pk); ?>


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        //'filterModel' => $searchModel,

        //'containerOptions' => ['class' => 'hotel-pjax-container'],

        //'options' => ['id' => 'hotel-pjax'],

        'columns' => [

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

            //['class' => 'yii\grid\SerialColumn'],


            'id',

            'filename:ntext',

            'originalname:ntext',

            'filetype',

            'filesize:ntext',


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

        ],

    ]); ?>


</div>


<?php ActiveForm::end(); ?>

<?php \yii\widgets\Pjax::end(); ?>


<?= Yii::$app->session->getFlash('error'); ?>


<? 

// Not working well see other topic: http://www.yiiframework.com/forum/index.php/topic/62417-shift-to-select-checkboxes/

// $this->registerJsFile('/js/shiftselect.js', ['depends' => [\yii\web\JqueryAsset::className()]], View::POS_END, 'my-options1');

?>


<?

$url = Yii::$app->urlManager->createUrl(['kruidvatm/files/', 'id' => '']);

?>


<?

$this->registerJs("

$('input[name^="."selection"."]').click(function(){

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

   console.log(keys);

 });", View::POS_END, 'my-options2');

?> 


<?

$this->registerJs("

$('#button2').click(function(){

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

console.log('".$url."'+keys);

console.log('hi');

window.location = '".$url."'+keys;

});", View::POS_END, 'my-options3');

?>


/*

controller:

*/


// replace the actionIndex controller with this:


public function actionIndex()

    {

        $searchModel = new KruidvatFilesSearch();

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


        $pk = Yii::$app->getRequest()->getQueryParams('id');

        $url = Yii::$app->urlManager->createUrl(['kruidvatm/files/']);

           

        if ($pk) {

           

           // Do Stuff with the Params...

 

           Yii::$app->getSession()->setFlash('error', 'Yes you've selected something '.$pk['id']);

            return $this->redirect($url);


            /*

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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

            'pk' => "Yes you've selected something".$pk['id'],

            ]);

            */

        } else {

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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

            'pk' => "Nope nothing selected",

        ]);

        }


    }




How to submit the form and get "the same" result.

Thanks in advance!