如何使用yii2中的复选框删除多行

时间:2016-05-20 12:14:41

标签: yii2-advanced-app

如何在GridView中使用删除所选对象,在Yii 2 Framework中如下图所示:

enter image description here

[在此输入图像说明] [2]

4 个答案:

答案 0 :(得分:2)

试试这个

<?=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();?> 

这是控制器:

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();
    }
    }

或者其他

按照此链接中给出的所有步骤,您一定会实现目标。 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/

答案 1 :(得分:1)

您可以为选中的每一行使用包含复选框和批量操作的列。

以下是相关问题:

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

答案 2 :(得分: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]: http://i.stack.imgur.com/iFjT1.png

答案 3 :(得分:0)

通过执行以下操作,我成功删除了gridview Yii2中的多行:

  1. 在index.php中创建按钮

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

  2. 在index.php中添加JavaScript代码,以执行从GridView小部件获取选中的行的事件。

    <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. 将此代码放入您的控制器

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