ajax delete button

I went through many topics and tried different codes, but not working…

I need "delete" button in my gridview.

On delete I need to delete file from folder (I need to call controller/action -> test/delete through ajax with parameter $data).

When success, it should update my gridview (if success it should call js code: ‘js:function(){$(\’#image-grid-view\’).yiiGridView.update(\‘image-grid-view\’);}

array(

    'class' => 'MyButtonColumn',


    'buttons' => array(


      'view' => array('visible' => 'false'),         


    ),        


    //'viewButtonUrl'=>


    //'updateButtonUrl'=>


    'deleteButtonUrl'=>'Yii::app()->createUrl("/test/delete", array("id" => $data))', //THIS IS NOT CORRENT, NEED AJAX CALL





  ),      

test/delete will take parameter $data that contains filename and delete given file from folder on server with given name.

Instead of calling another action in controller, how about putting your delete procedure in beforeDelete/afterDelete event (in your model)?

If I understand correctly, you can use Yii’s default CGridView delete command. In your controller, create a private method deleteFile. Since Yii’s CGridView’s delete command is called via Ajax, you don’t need to code anything yourself. Just call deleteFile() from actionDelete().




public function actionDelete($id)

{

    $model = $this->loadModel($id);

    $filename = $model->filename;


    if ($model->delete()

    {

         $this->deleteFile($filename);

    } 


    ...

}


private function deleteFile($filename)

{

     try

     {

         unlink($filename);

     }

     catch()

     {

...



Matt