0

I need bulk delete for my grid. I have jquery script

  $activeMassScriptJS= <<<JS
  $(document).ready(function(){
  $("button.product-update").on('click',function(e){
            e.preventDefault;
            var keys = $('#products-grid').yiiGridView('getSelectedRows');
            $.get('mass-status-movie', {keylist : keys}, function(data) { alert(keys[0]);});
            return false;
    });
});

JS;

controller

public function actionMassdelete(){
    if (Yii::$app->request->post('keylist')) {
        $keys = Yii::$app->request->post('keylist');
        foreach ($keys as $key) {
            $model = Product::findOne($key);
            $model->delete();
        }
    }
    return $this->redirect(Url::previous());
}

it working for address .../movie/index but i need to working for address /movie/index?parameter=value how to do?

4
  • I can't quite understand your situation. As I got it, your JS is running at /movie/index and you need to send GET-request to /movie/index?parameter=value, right? But you have another URI in your script: $.get('mass-status-movie'... And the action provided is /something/massdelete... Commented Dec 25, 2015 at 16:04
  • Yes, i need sent get request. massdelete will remove all items from grid. Commented Dec 25, 2015 at 16:22
  • Well, you're sending the request not to the action you've provided. Commented Dec 25, 2015 at 16:43
  • I know, i should use get method everywhere, but i don't know how use extra parameter in controller Commented Dec 25, 2015 at 16:44

1 Answer 1

1

You seem to have confusing code. Your code is making a get request to the server, but then your action, which is not the one you sent the request to, is checking post parameters! You need to sort out what requests you are making, and check the values accordingly. Here are two scenarios;

Case 1 You keep the ajax call as a get request, so yor code is

$activeMassScriptJS= <<<JS
$(document).ready(function(){
$("button.product-update").on('click',function(e){
    e.preventDefault;
    var keys = $('#products-grid').yiiGridView('getSelectedRows');
    $.get('mass-status-movie', {keylist : keys}, function(data) { alert(keys[0]);});
    return false;
    });
});

The values that you have sent via the get request will now be available in two ways. At the action you pointed to, i.e.actionMassStatusMovie(), you can access them like this;

public function actionMassStatusMovie($keyList){
    //Your code here
}

You would use this if you always wanted the keyList parameter to be present.

You can also access the get parameters anywhere in your script like this Yii::$app->request->get('keylist'). In this case, you don't need the $keyList parameter, so you can just use

public function actionMassStatusMovie(){
        $keyList = Yii::$app->request->get('keylist');
    }

Case 2

Alternatively, you can send the data via a post request. In this case, you don't use the parameter, just check it like you have been doing,

public function actionMassStatusMovie(){
        $keyList = Yii::$app->request->post('keylist');
    }

But to do this, you need to change your original javascript code to make a post request.

 $activeMassScriptJS= <<<JS
  $(document).ready(function(){
  $("button.product-update").on('click',function(e){
            e.preventDefault;
            var keys = $('#products-grid').yiiGridView('getSelectedRows');
            $.post('mass-status-movie', {keylist : keys}, function(data) { alert(keys[0]);});
            return false;
    });
});

get and post requests are different, and your code needs to be consistent in how they are being used!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.