Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

php - Yii 2 : how to bulk delete data in kartik grid view?

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

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

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...