CGridView - add custom function to the delete row button

PartySoft picture PartySoft · May 11, 2012 · Viewed 8.5k times · Source

By default the delete button assigns itself to this function:

jQuery('#main-orders-details-grid a.delete').live('click',function() {
    if(!confirm('Are you sure you want to delete this item?')) return false;
    var th=this;
    var afterDelete=function(){};
    $.fn.yiiGridView.update('main-orders-details-grid', {
        type:'POST',
        url:$(this).attr('href'),
        success:function(data) {
            $.fn.yiiGridView.update('main-orders-details-grid');
            afterDelete(th,true,data);
        },
        error:function(XHR) {
            return afterDelete(th,false,XHR);
        }
    });
    return false;
});

Is there any way to override this function and add a user defined one?

I'm having some troubles with some nested grids - it updates the master grid when I delete a row from the child.

Answer

pere_ picture pere_ · May 11, 2012

Of course you can :). I assume that you already have a CButtonColumn class column defined. You can do it like so:

        array(
            'class'=>'CButtonColumn',
            'template'=>'{view}{delete}',
            'buttons'=>array(
                'view'=>array(
                    'url'=>'Yii::app()->createUrl("controller/action",array("id"=>$data>id))',
                 ),
                'delete'=>array(
                    'url'=>'#',
                    'click'=>'js:function(evt){
                        evt.preventDefault();
                        /*Your custom JS goes here :) */
                        }',                        
                ),                    
            ),
        ),

Also check out Yii's doc here: http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail