Confirmation message when click the delete button in CGridview

Hi all,

     I am new to yii.I want to display the confirmation message when delete button clicked.for that i am wrote 

the following code.

sponsor.php in view

          <?php 


  $this->widget('zii.widgets.grid.CGridView', array(


 


   'htmlOptions'=>array('class'=>'box_content'),


   'dataProvider'=>$dataProvider,


  'summaryText'=>'',


   'itemsCssClass'=>'sorting',


  


  'columns'=>  


    array(


     array(


      'class'=>'CCheckBoxColumn',


  


     


    ),


     


         'id',


        'name',


       'address',


    


    array(


     'class'=>'CButtonColumn',


    'deleteConfirmation'=>"Are You Sure?",


     'header'=>'tools',


     'htmlOptions'=>array('class'=>'tools'),


    


	 'template'=>'{view}{update}{delete}',


   


'buttons'=>array


(


 


    'view' => array


    (


        'label'=>'View',


    'imageUrl'=>Yii::app()->request->baseUrl.'/images/icon_tool_view.png',


        'url'=>'#',


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


    


    ),


     'update' => array


    (


        'label'=>'Update',


    'imageUrl'=>Yii::app()->request->baseUrl.'/images/icon_tool_edit.png',


        'url'=>' Yii::app()->createUrl("admin/update", array("id"=>$data["id"]))',


      


        'options'=>array('class'=>'edit'),


    ),


    'delete' => array


    (


        'label'=>'Delete',


    'imageUrl'=>Yii::app()->request->baseUrl.'/images/icon_tool_delete.png',


         'url'=>'#',


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


     


    ),


	 ),


    ),


  


    


    ),


    )); ?>

My Model class is as follows

<?php

Class Sponsor extends CModel

{

    public function attributeNames()


       {


          


       	return array('name'=>'Name','address'=>'Address');		





       }


     


    public function getallsponsor()


      


      {


	      $connection=yii::app()->db;


	      $dataReader=$connection->createCommand("CALL sp_getall_sponsor()")->query();


	      $rows=$dataReader->readAll();


	      return ($rows);


      } 


      


    public function getbyid($id)





      {


      	  $connection=yii::app()->db;


	      $dataReader=$connection->createCommand("CALL sp_getbyid_sponsor($id)")->query();


	      $rows=$dataReader->readAll();


	      return ($rows);


      }

}

My Controller code is

<?php

class AdminController extends Controller

{

public &#036;layout='//layouts/admin';


public function actions()


{


return array(


'page'=&gt;array(


			'class'=&gt;'CViewAction',	),);


}


public function actionIndex()


{


	


	&#036;this-&gt;render('index');


}





public function actionSponsor()


{





	 &#036;model=new Sponsor;


	


     &#036;rawData=&#036;model-&gt;getallsponsor();


    





     &#036;dataProvider=new CArrayDataProvider(&#036;rawData);


     &#036;this-&gt;render('sponsor', array(


	 'dataProvider' =&gt; &#036;dataProvider));


	


}

}

The above code display the grid view with the corresponding information.But when i click the  delete button.

it did not display the confirmation message.I am not able find what the mistake.if anybody knows pls reply to this.

Thanks in advance.

I don’t see an actionDelete in your controller?!

Look at the code that is generated - when you delete it sends a POST to /yourcontroller/delete/id and you need to have an actionDelete method to handle it.

Best bet is to use Gii to generate another controller and use the sample code in your controller.

Thanks for the quick reply.To delete the row in CGridView we need the controller action.But the delete confirmation is related with CButtonColumn.So to display just the confirmation message box,is controller action required?

See this wiki article

Thanks for your reply.I wrote my code after refer this article.

      'class'=&gt;'CButtonColumn',


      [b]'deleteConfirmation'=&gt;'Are You Sure?',[/b]


      'header'=&gt;'tools',


      'htmlOptions'=&gt;array('class'=&gt;'tools'),


    


	 'template'=&gt;'{view}{update}{delete}',


   


'buttons'=&gt;array


(


 


    'view' =&gt; array


    (


        'label'=&gt;'View',


    'imageUrl'=&gt;Yii::app()-&gt;request-&gt;baseUrl.'/images/icon_tool_view.png',


        'url'=&gt;'yii::app()-&gt;createUrl(&quot;admin/view&quot;)',


        'options'=&gt;array('class'=&gt;'view')


    


    ),


     'update' =&gt; array


    (


        'label'=&gt;'Update',


    'imageUrl'=&gt;Yii::app()-&gt;request-&gt;baseUrl.'/images/icon_tool_edit.png',


        'url'=&gt;' Yii::app()-&gt;createUrl(&quot;admin/update&quot;, array(&quot;id&quot;=&gt;&#036;data[&quot;id&quot;]))',


      


        'options'=&gt;array('class'=&gt;'edit'),


    ),


    'delete' =&gt; array


    (


        'label'=&gt;'Delete',


    'imageUrl'=&gt;Yii::app()-&gt;request-&gt;baseUrl.'/images/icon_tool_delete.png',


         'url'=&gt;'Yii::app()-&gt;createUrl(&quot;admin/delete&quot;, array(&quot;id&quot;=&gt;&#036;data[&quot;id&quot;]))',


        'options'=&gt;array('class'=&gt;'delete'),


     


    ),


	 ),


    


  But it did not show the message box.Now i change that into





                [b]'deleteConfirmation'=&gt;&quot;js:'Are You Sure?'&quot;,[/b]

But it also not working :(

try adding this line to delete buttons column


'click'=>'function(){return confirm("are you sure ?");}'

Thanks for your reply.

I try this one.But it does not work.

The default code generated with Gii has the admin section with a CGridView… and there the delete button should work… so try to create a model and CRUD for your table with Gii… to see if that will work for you…

Thanks for your reply mdomba.

The gii create the model which extend the CActiveRecord.But i want to extend the CModel class.So i am not able to use gii generator.

Why not try to create it just to see if the delete button works so you can study how it’s implemented and how it works…