Disabling 'delete' Button In Cbuttoncolumn

I am able to disable ‘delete’ in CButtonColumn if I am doing it like this


array(

      'class'=>'CButtonColumn',

       'buttons'=>array

			('delete'=> array

                                ( 'url' => 'Yii::app()->createUrl("groupassign/delete/", array("groupID"=>$data->groupID, "memberID"=>$data->memberID))', 

                                  'visible'=>'Yii::app()->user->name ==  Group::model()->findByPk($data->groupID)->groupOwner'

                                  ),

But if I do it like this it is not working giving error "Undefined variable Data"


'visible'=>$groupassign->checkDeletePermission($data->groupID, $data->memberID)

where checkDeletePermission function is in my model. What mistake I am making in 2nd part? I want to keep checking condition for ‘visible’ parameter of ‘delete’ button in my model instead of view.

You need to quote the statement for [font=“Courier New”]visible[/font] with single quotes so it’ll be evaluated during grid rendering.

thanks for the reply. so you mean I should do it like this


'visible'=>'$groupassign->checkDeletePermission($data->groupID, $data->memberID)'

But then also it is not working.

try this


'template'=>'{update}';

Ah, I see. I guess [font=“Courier New”]$groupassign[/font] can’t be resolved?

I have already included


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

Yes $groupassign can’t be resolved. How can I solve this problem?

I’m not quite sure. Is your PHP recent enough to support anonymous functions? If you cannot find a way to resolve [font=“Courier New”]$groupassign[/font] within your expression, that were the way to go.

try this


'url' => 'Yii::app()->createUrl(\'groupassign/delete\', array(\'groupID\'=>$data->groupID, \'memberID\'=>$data->memberID))',

ok if I directly put condition in view then is it a safe practice?


'visible'=>'Yii::app()->user->name ==  Group::model()->findByPk($data->groupID)->groupOwner'

Yes, that should be fine.

also I am using latest php version so how can I do it with anonymous function?

How this will resolve ‘visible’ problem? I have no problem with ‘url’ it is working fine.


'url' => 'Yii::app()->createUrl("groupassign/delete/", array("groupID"=>$data->groupID, "memberID"=>$data->memberID))'

See here.

I finally found the solution for disabling ‘delete’ button (keeping checking condition for ‘visible’ parameter in the model instead of view) using anonymous function.


'visible'=>function($row,$data) use(&$groupassign) {return $groupassign->checkDeletePermission($data->groupID);}

Thanks to Da:Sourcerer for his suggestion.