3

How can I use in GridView delete selected object,in Yii 2 Framework such as following image:

enter image description here

[enter image description here][2]

4 Answers 4

3

Try this

<?=Html::beginForm(['controller/bulk'],'post');?>
<?=Html::dropDownList('action','',[''=>'Mark selected as: ','c'=>'Confirmed','nc'=>'No Confirmed'],['class'=>'dropdown',])?>
<?=Html::submitButton('Send', ['class' => 'btn btn-info',]);?>
<?=GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
'id',
],
]); ?>
<?= Html::endForm();?> 

This is the controller:

public function actionBulk(){
    $action=Yii::$app->request->post('action');
    $selection=(array)Yii::$app->request->post('selection');//typecasting
    foreach($selection as $id){
        $e=Evento::findOne((int)$id);//make a typecasting
        //do your stuff
        $e->save();
    }
    }

Or Else

Follow all the steps given in this Link, You will Surely achive your goal. Yii 2 : how to bulk delete data in kartik grid view?

https://stackoverflow.com/questions/27397588/yii-2-how-to-bulk-delete-data-in-kartik-grid-view/
Sign up to request clarification or add additional context in comments.

21 Comments

one issue is image not display :( i ask one thing yii2 advanced not give permission access image in Back-end directory image direct ???
Use Web Directory to upload image. $img = Url::to('@web/uploads/PROJECT/').$img_obj['AVATAR']; $image = '<img src="'.$img.'" width="600" />'; <img src="<?= Yii::$app->request->baseUrl . '/backend/web/uploads/' . $model->profile_photo ?>" class=" img-responsive" > <?php echo Html::img('@web/img/icon.png', ['class' => 'pull-left img-responsive']); ?>
how multiple row delete using checkbox in yii2 this issue can not solved please help
what kind of issues are you facing ?? pls tell error message.
how multiple row delete using checkbox in yii2 ye issue solve ho gaya bro thanks @vijay bhai
|
1

You can use a column with checkboxes and bulk actions for each row selected.

Here is a related question:

Yii2 How to properly create checkbox column in gridview for bulk actions?

Comments

1

enter image description here

<?php
$url = Url::to(['user/delete']);
$this->registerJs('
     $(document).on("click", "#delete_btn",function(event){
     event.preventDefault();
       var grid = $(this).data(\'grid\');
        var Ids = $(\'#\'+grid).yiiGridView(\'getSelectedRows\');
        var status = $(this).data(\'status\');
        if(Ids.length > 0){
        if(confirm("Are You Sure To Delete Selected Record !")){
              $.ajax({
                type: \'POST\',
                url :  \''.$url.'\' ,
                data : {ids: Ids},
                dataType : \'JSON\',
                success : function($resp) {
                if($resp.success){
                 alert(resp.msg);
                }
                }
            });
        }
        }else{
        alert(\'Please Select Record \');
}
    });
    ', \yii\web\View::POS_READY);
?>


  [1]: https://i.sstatic.net/iFjT1.png

1 Comment

This is answer may work but vulnerable to csrf attack.
0

I have succeeded in deleting multiple rows in gridview Yii2 by doing the following:

  1. Create button in index.php

    <p> <button type="button" onclick="getRows()" class="btn btn-success">Delete Bulk</button> </p>

  2. Add javascript code in index.php to perform the event of getting the checked rows from the GridView widget.

    <script> function getRows() { //var user_id as row_id from the gridview column // var list = [] is an array for storing the values selected from the //gridview // so as to post to the controller. var user_id; var list = []; //input[name="selection[]"] this can be seen by inspecting the checkbox from your //gridview $('input[name="selection[]"]:checked').each(function(){ user_id = this.value; list.push(user_id); }); $.ajax({ type: 'post', url:'index.php?r=student-detail-update/bulk', data: {selection: list}, }); } </script>

  3. Put this code in your contoller

    if ($selection=(array)Yii::$app->request->post('selection')) { foreach($selection as $id){ $StudentDetailUpdates = StudentDetailUpdate::find() ->where(['user_id' => $id]) ->all(); //....put your staff here }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.