Cbuttoncolumn, Adding Button With Confirmation

Hey guys,

i’ve been having a problem trying to add a custom button to the CButtonColumn in my CGridView.

Adding the button works fine, but when i try to add the ‘click’ option, things start going messy.

what i have is this:


array(

                    'class'=>'CButtonColumn',

                    'template' => '{view}{update}{delete}{activate}',  //include the standard buttons plus the new status button

                    'htmlOptions'=> array('style'=>'width:80px'),

                    'deleteConfirmation'=>"js:'Wilt u de account van '+$(this).parent().parent().children(':nth-child(1)').text()+' '+$(this).parent().parent().children(':nth-child(2)').text()+' verwijderen ?'",

                    'buttons'=>array(

                        'activate'=>array(

                            'visible'=>'$data->role=="inactive"',

                            'label'=>'activate',

                            'imageUrl'=>'images/icon/activate.png',

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

                          

                        )

                    )

                )

this works fine, but when i try add


'click'=>'activateFunction(){alert("hey there");}',

to the activate button, it doesn’t work

even more, it breaks my delete button, giving me an ‘invalid request’ error

i’ve been at this for days, i don’t get it

can anyone help me out ?

found my problem … appearently you can’t just call the function whatever you want, it has to be called ‘function(){//insert code}’

looks like a silly mistake :)

ok, now my second problem rises

my deleteConfirmation looks like this


'deleteConfirmation'=>"js:'Wilt u de account van '+$(this).parent().parent().children(':nth-child(1)').text()+' '+$(this).parent().parent().children(':nth-child(2)').text()+' verwijderen ?'",



now when i try to add similar code to my ‘click’ function for the activate button, there is the problem of double and single quoting

my solution was


'click'=><<<EOD


                                function(){

                                confirm("'Wilt u de account van '+$(this).parent().parent().children(':nth-child(1)').text()+' '+$(this).parent().parent().children(':nth-child(2)').text()+' activeren ?'")

                                }

EOD

now this doesn’t break anything anymore, but it literally displays the text, instead of interpreting .parent().parent() and so on

i know this is just a matter of getting my quotes straight, but i could really use some help

ok, i eventually managed to fix it

complete code is like this


		array(

                    'class'=>'CButtonColumn',

                    'template' => '{view}{update}{delete}{activate}',  //include the standard buttons plus the new status button

                    'htmlOptions'=> array('style'=>'width:80px'),

                    'deleteConfirmation'=>"js:'Wilt u de account van '+$(this).parent().parent().children(':nth-child(1)').text()+' '+$(this).parent().parent().children(':nth-child(2)').text()+' verwijderen ?'",

                    'buttons'=>array(

                        'activate'=>array(

                            'visible'=>'$data->role=="inactive"',

                            'label'=>'activate',

                            'imageUrl'=>'images/icon/activate.png',

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

                            'click'=><<<EOD


                                function(){

                                confirm('Wilt u de account van '+$(this).parent().parent().children(':nth-child(1)').text()+' '+$(this).parent().parent().children(':nth-child(2)').text()+' activeren ?')

                                }

EOD

in case anyone else would encounter the same problem :)

Incase anyone was wondering there is a more elegant approach to this situation.




'buttons' => array(

                    'remove' => array(

                        'options' => array(

                            'confirm' => 'Are you sure?',

                        ),

                        'label' => 'Remove',

                        'imageUrl' => ...

                        'url' => '...'

                    ),

                ),



And incase you want to add $data properties to the option you can follow this wiki

http://www.yiiframework.com/wiki/372/cbuttoncolumn-use-special-variable-data-for-the-id-in-the-options-of-a-button/

Found this via google and came up with an imo better solution.

Hi,

please see it…

http://www.yiiframework.com/wiki/553/how-to-change-the-confirmation-message-on-admingridview-button-and-call-a-ajax-dynamic/