Search code examples
ajaxyiicgridview

Yii CgridView bulk delete


In many of my Models' CgridViews I have a bulk delete function: a chechboxColumn and a delete button which deletes all the checked users. For that I am using ajax in the admin and a new action in the controller.

All this works fine until I add pagination to th gridview, which is not saving the checked rows in the previous pages.

I tried to use 'enableHistory'=true, but it did nothing (and from what I'v read I'm not the only one :mellow: ) , so I downloaded this extension: selgridview

The extension works - when I move through the pages , the checked rows stay checked BUT , my bulk delete function is seeing only the checked rows of the page I'm in right now.

this is the ajax I'm using:

        <?php
    Yii::app()->clientScript->registerScript('delete','
    $("#butt").click(function(){
                    var checked=$("#person-grid").yiiGridView("getChecked","person-grid_c11");
                    var count=checked.length;
                    if(count>0 && confirm(" are you sure you want to delete "+count+" people ? "))
                    {
                                    $.ajax({
                                                    data:{checked:checked},
                                                    url:"'.CHtml::normalizeUrl(array('person/remove')).'",
                                                    success:function(data){$("#person-grid").yiiGridView("update",{});},              
                                    });
                    }
                    });
    ');
    ?>

Now , maybe thats a silly question but I know little about javascript. I'm not even sure that the problem is in the ajax . . . .

Help would be much appreciated :rolleyes:


Solution

  • I am using selgridview extension.

    Here is my code for deleting the selected users

    //delete multiple users at once
    $('#delete_selected_items_button').on('click', function () {
        var selected = $("#users-grid").selGridView("getAllSelection");
    
        //if nothing's selected
        if ( ! selected.length)
        {
            alert('Please select minimum one user to be deleted');
            return false;
        }
    
        //confirmed?
        if ( ! confirm('Are you sure to delete ' + selected.length + ' users?')) return false;
    
        var multipledeleteUrl = "<?php echo Yii::app()->baseUrl;?>/users/multipledelete";
    
        $.ajax({
            type: "POST",
            url: multipledeleteUrl,
            data: {selectedUsers : selected},
            success: (function (e){
    
                //just to make sure we delete the last selected items
                $("#users-grid").selGridView("clearAllSelection");
    
                //we refresh the CCGridView after success deletion
                $.fn.yiiGridView.update("users-grid");
    
            }),
            error: (function (e) {
                alert("Can not delete selected users");
            })
        });
    })
    

    On UsersController, actionMultipleDelete() do something like this

    if (Yii::app()->request->isAjaxRequest)
            {
                $selectedUsers = Yii::app()->request->getPost('selectedUsers');
    
                //iterate through all ids
                foreach ($selectedUsers as $id)
                {
                    //delete the user here...
                }
            }