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?
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!