Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Kartik grid view in yii2 provides an option to display checkboxes in grid view. How do I delete bulk data by checking the checkboxes? Any help would be greatful. Here is my code:

<?php use kartikgridGridView;?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'pjax'=>true,
    'pjaxSettings'=>[
        'neverTimeout'=>true,
    ],
    'columns' => [
        ['class' => 'kartikgridCheckboxColumn'],
        ['class' => 'yiigridSerialColumn'],

        'hotel_id',
        'name',
        'address',
        'phone_no',
        'contact_person',
        ['class' => 'yiigridActionColumn'],
    ],
]); ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
624 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this , i hope this will help you, instead of this kartikgridCheckboxColumn use this yiigridCheckboxColumn

Step 1: create a button for multiple delete 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
            }
        });

    });
    });', yiiwebView::POS_READY);

?>

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

Step 3: in controller of hotel (HotelController.php) create a action for multiple-delete

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 this surely this will help you...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...