Confirmation on click update in CGrid

Confirmation on click update in CGrid

I swam in the code for 3 hours before. This is a way I found to add a javascript confirm box to a CGrid customized update button.

1- Add the yii app action you want to call and the confirmation javascript function at the beginning of the view with the CGrid that you want to customize





<?php

#myview with special confirm update CGrid

//Initialiser l'action du controleur... change this to your controller action

$actionURL = Yii::app()->createUrl("inscriptions/register");

//Ajouter le script pour traiter l'action en AJAX  ... idcontexte is the ID from de CGrid column... change this to whatever you need  

Yii::app()->clientScript->registerScript('confirmation', "        

    function confirmation(idcontexte){

        var choix = false;

        choix = confirm('Cette opération réinitialisera tous vos tirs pour ce contexte\\nVoulez-vous vraiment continuer?');

        if(choix==true){

            $.ajax({

                    type: \"POST\",

                    url: \"$actionURL \",

                    data: \"idcontexte=\"+idcontexte,

                    success:alert('Initialisation des tirs complété'),

                    error:function(x,e){

                                        if(x.status==0){

                                            alert('You are offline!!\\n Please Check Your Network.');

                                        }else if(x.status==404){

                                            alert('Requested URL not found.');

                                        }else if(x.status==500){

                                            alert('Internel Server Error.');

                                        }else if(e=='parsererror'){

                                            alert('Error.\\nParsing JSON Request failed.');

                                        }else if(e=='timeout'){

                                            alert('Request Time out.');

                                        }else {

                                            alert('Unknow Error.\\n'+x.responseText);

                                        }

                                    }

                    });

            }

        else

            return false;

    }

");

?>




2- Add click event confirmation function with special parameter to the button in the grid where you want to confirm AND remove the url to disable the default action of the button.





array(

			'class'=>'CButtonColumn',                        

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

                        'buttons'=>array(

                            'register'=>array(

                                        'label'=>'S\'inscrire',

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

                                        'click'=>"function(){confirmation($(this).parent().parent().children(':nth-child(1)').text())}",

                                        //'url'=>'Yii::app()->createUrl("inscriptions/register?idcontexte=$data->idcontexte")', ... this line can be remove

                                

                                        

                                        ),

                            'update'=>array(

                                        'label'=>'Mettre à jour',

                                        'url'=>'Yii::app()->createUrl("contexte/updateOwn/$data->idcontexte")',

                                        'visible'=>'(Yii::app()->user->idmembres==$data->idmembre?true:false)',

                                        ),

                            'delete'=>array(

                                        'label'=>'Supprimer',

                                        'url'=>'Yii::app()->createUrl("contexte/deleteOwn/$data->idcontexte")',

                                        'visible'=>'(Yii::app()->user->idmembres==$data->idmembre?true:false)',

                                        ),                            

                            'view'=>array(

                                        'label'=>'Afficher',

                                        'url'=>'Yii::app()->createUrl("contexte/viewOwn/$data->idcontexte")',

                                        ),                            

                            

                            ),




THINGS YOU HAVE TO KNOW

All this works because of this parameter in the javascript function : [b]


$(this).parent().parent().children(':nth-child(1)').text()

[/b]

This parameter get the first CGrid column value. (in my situation this is the ID) If you need to get second or third parameter you just have to change the child number.

Of course you need a controller with the action you want to call : [b]


$actionURL = Yii::app()->createUrl("inscriptions/register");

[/b]

This parameter call the register action in the inscription controller. Change this to whatever you need to confirm before execute

If every things works, you will have a javascript confirmation box before the action and an alert box return message for success or error

Confirmation

Alert message

If you need more details you can write to me.

Good Look !

Email me