Yii 2: how to combine data deletion as a picture grid?

The Kartik grid grid in yii2 provides the ability to display checkboxes as a grid. How to delete bulk data by checking the boxes? Any help would be helpful. Here is my code:

<?php use kartik\grid\GridView;?> <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'pjax'=>true, 'pjaxSettings'=>[ 'neverTimeout'=>true, ], 'columns' => [ ['class' => '\kartik\grid\CheckboxColumn'], ['class' => 'yii\grid\SerialColumn'], 'hotel_id', 'name', 'address', 'phone_no', 'contact_person', ['class' => 'yii\grid\ActionColumn'], ], ]); ?> 
+8
php yii2
source share
2 answers

1. Add custom class or id to pjax container

Either add a class or id to your pjax container with a GridView, so that you are not dependent on automatically generated classes and identifiers (or if you have several GridView widgets on the same page).

kartik\grid\CheckboxColumn is just an extended version of yii \ grid \ CheckboxColumn .

kartik\grid\View has containerOptions , you can specify a class here, it seems that id automatically generated and cannot be changed using this property.

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

An example of the generated output:

 <div class="hotel-pjax-container table-responsive" id="w0-container">...</div> 

yii\grid\View\ has options , here you can specify id . The identifier of the result container will be the prefix of the passed value, for example:

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

Generated Output:

 <div class="table-responsive" id="hotel-pjax-container">...</div> 

The class is ignored in this case.

I recommend specifying an identifier.

2. Create or modify an action for deletion in the controller

By default, the delete action generated by gii is redirected, so we can create another action for multiple deletion (or you can handle it in one, it is 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]); } 

Please note that if you did not specify any condition in deleteAll() , all entries in the table will be deleted! Be precise with that.

You can also specify an action in VerbFilter as follows:

 use yii\filters\VerbFilter; /** * @inheritdoc */ public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], 'delete-multiple' => ['post'], ], ], ]; } 

3. Write javascript to link everything together

You can get the primary keys of selected rows as follows:

 $('#hotel-pjax-container').yiiGridView('getSelectedRows'); 

Add this javascript (e.g. to a button):

 $.post( "delete-multiple", { pk : $('#hotel-pjax-container').yiiGridView('getSelectedRows') }, function () { $.pjax.reload({container:'#hotel-pjax-container'}); } ); 

For more information on updating GridView with pjax, see issue . Perhaps try this: $('#hotel-pjax-container').yiiGridView('applyFilter'); as an alternative; Include js using assets, or simply using registerJs() ;

+7
source share

Try this, I hope this helps you, instead kartik\grid\CheckboxColumn use this yii\grid\CheckboxColumn

Step 1: create a button for multiple deletion in index.php

 <input type="button" class="btn btn-info" value="Multiple Delete" id="MyButton" > 

Step 2: In your index.php (same page) use this javascript

 <?php $this->registerJs(' $(document).ready(function(){ $(\'#MyButton\').click(function(){ var HotId = $(\'#w4\').yiiGridView(\'getSelectedRows\'); $.ajax({ type: \'POST\', url : \'index.php?r=hotel/multiple-delete\', data : {row_id: HotId}, success : function() { $(this).closest(\'tr\').remove(); //or whatever html you use for displaying rows } }); }); });', \yii\web\View::POS_READY); ?> 

this jquery is used to get the value (id) of the selected row

Step 3: in the hotel manager (HotelController.php) create an action for multiple deletion

 public function actionMultipleDelete() { $pk = Yii::$app->request->post('row_id'); foreach ($pk as $key => $value) { $sql = "DELETE FROM hotel WHERE hotel_id = $value"; $query = Yii::$app->db->createCommand($sql)->execute(); } return $this->redirect(['index']); } 

Try it, it will help you ...

+7
source share

All Articles