Bulk Delete And Pagination

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: http://www.yiiframework.com/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:

The docs for selgridview says to use


$("#myId").selGridView("getAllSelection")

It looks like you are using yiiGridView() rather than selGridView().


It looks like you are using yiiGridView() rather than selGridView(). 

Huh , Indeed I was.

Thank you!

Here is the js from one of my views that does the selgridview delete process for me. Basically I just copied the delete process for the default CGridView for the most part.


$("#bulk-delete").on("click", function() {

    var selected = $("#image-gridview").selGridView("getAllSelection");

    if(selected.length > 0) {

        if(!confirm("Are you sure you want to delete these items?")) return false;

        $("#image-gridview").selGridView("clearAllSelection");

        var th = this,

            afterDelete = function(){};

        $("#image-gridview").yiiGridView("update", {

            data: ({selected:selected}),

            url: "' . app()->createUrl('images/bulkDelete') . '",

            success: function(data) {

                $("#image-gridview").yiiGridView("update");

                afterDelete(th, true, data);

            },

            error: function(XHR) {

                return afterDelete(th, false, XHR);

            }

        });

        return false;

    }

});

And then my controller action is just a basic


$ids = app()->request->getParam('selected');

        foreach($ids as $id) {

            $this->loadModel($id)->delete();

        }