1. Add custom class or id to pjax container
Either add class or id to your pjax container with GridView, so you are not depending from auto generated classes and ids (or in case you have multiple GridView widgets in one page).
kartik\grid\CheckboxColumn
is just extended version of yii\grid\CheckboxColumn.
kartik\grid\View
has containerOptions
, you can specify class
here, it seems like id
is auto generated and can not be changed using this property.
'containerOptions' => ['class' => 'hotel-pjax-container'],
Example of generated output:
<div class="hotel-pjax-container table-responsive" id="w0-container">...</div>
yii\grid\View\
has options
, you can specify id
here. The result container id will be prefixed with passed value, for example:
'options' => ['id' => 'hotel-pjax'],
Generated output:
<div class="table-responsive" id="hotel-pjax-container">...</div>
Class is ignored in this case.
I recommend specifying id.
2. Create or modify action for deletion in controller
By default delete
action auto generated with gii
has redirect, so we can create
another action for multiple deletion (or you can handle this in one, it's up to you).
public function actionDeleteMultiple()
{
$pk = Yii::$app->request->post('pk'); // Array or selected records primary keys
// Preventing extra unnecessary query
if (!$pk) {
return;
}
return Hotel::deleteAll(['hotel_id' => $pk]);
}
Note that if you didn't specify any condition in deleteAll()
, all records in the table will be deleted! Be accurate with that.
You can also specify the action in VerbFilter
like this:
use yii\filters\VerbFilter;
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
'delete-multiple' => ['post'],
],
],
];
}
3. Write some javascript to tie all together
You can get primary keys of selected rows like this:
$('#hotel-pjax-container').yiiGridView('getSelectedRows');
Add this javascript (to the button click for example):
$.post(
"delete-multiple",
{
pk : $('#hotel-pjax-container').yiiGridView('getSelectedRows')
},
function () {
$.pjax.reload({container:'#hotel-pjax-container'});
}
);
You can find more information about updating the GridView with pjax in this issue.
Maybe try this: $('#hotel-pjax-container').yiiGridView('applyFilter');
as alternative;
Include js using assets or just with registerJs()
;