CGridView add another delete button with ajax post request

netuser24 picture netuser24 · Mar 26, 2012 · Viewed 8.7k times · Source

Hello is it possible to add another delete button to Yii CGridView? I would like it to make an ajax post request to one of mine controllers.

An use case would be:

Lets say I have a blog entry with comments. In administration I would like to have a gridview where I can delete this blog entry (this is already) possible. I would also like to have another delete button next to the original delete button which would only delete blog's comments (which I don't know how to add).

I looked over the HTML generated by Yii framework to see how the original delete button works. One possible solution would be to copy/paste that delete and refresh java script code and modify it a bit. But I don't like this. I have some concerns that if I do this, when I upgrade Yii framework there is no guarantee that my solution will still work.

Answer

Speedy picture Speedy · Jul 10, 2012

Use above tutorial and add class="delete" to the new button

'options'=>array('class'=>'delete')

Yii framework assigns ajax call to this class.

array('class'=>'CButtonColumn','template'=>'{remove}{view}{update}{delete}',
          'htmlOptions'=>array('style'=>'width:65px'),
          'buttons'=>array(
            'remove'=>array('url'=>'Yii::app()->createUrl("resolution/removeApp", array("id_application"=>$data->id_application,"id_resolution"=>'.$model->id_resolution.'))','label'=>'Remove application from resolution.','imageUrl'=>Yii::app()->request->baseUrl.'/images/remove.png','options'=>array('class'=>'delete')),
            ))