首页 > PHP资讯 > PHP培训技术 > Yii框架下自定义CGridView中CButtonColumn的解决方案

Yii框架下自定义CGridView中CButtonColumn的解决方案

PHP培训技术

因为项目需要,需要给产品添加上架下架的功能,而最方便的方案就是在产品管理界面admin.php下给 ::import('zii.widgets.grid.CButtonColumn');class PButtonColumn extends CButtonColumn{public $template='{view} {update} {publish} {offline} {delete}';public $publishButtonLabel;public $publishButtonImageUrl;public $publishButtonUrl='Yii::app()->controller->createUrl("publish",array("id"=>$data->primaryKey))';public $publishButtonOptions=array('class'=>'publish');public $afterPublish;public $offlineButtonLabel;public $offlineButtonImageUrl;public $offlineButtonUrl='Yii::app()->controller->createUrl("offline",array("id"=>$data->primaryKey))';public $offlineButtonOptions=array('class'=>'offline');public $offlineConfirmation;public $afterOffline;protected function initDefaultButtons(){if($this->viewButtonLabel===null)$this->viewButtonLabel='查看';if($this->updateButtonLabel===null)$this->updateButtonLabel='修改';if($this->publishButtonLabel===null)$this->publishButtonLabel='发布';if($this->offlineButtonLabel===null)$this->offlineButtonLabel='下架';if($this->deleteButtonLabel===null)$this->deleteButtonLabel='删除';if($this->viewButtonImageUrl===null)$this->viewButtonImageUrl=$this->grid->baseScriptUrl.'/view.png';if($this->updateButtonImageUrl===null)$this->updateButtonImageUrl=$this->grid->baseScriptUrl.'/update.png';if($this->publishButtonImageUrl===null)$this->publishButtonImageUrl = $this->grid->baseScriptUrl.'/publish.png';if($this->offlineButtonImageUrl===null)$this->offlineButtonImageUrl = $this->grid->baseScriptUrl.'/offline.png';if($this->deleteButtonImageUrl===null)$this->deleteButtonImageUrl=$this->grid->baseScriptUrl.'/delete.png';if($this->offlineConfirmation===null)$this->offlineConfirmation='您确定要下架这个产品吗?';if($this->deleteConfirmation===null)$this->deleteConfirmation='您确定要删除这个产品吗?这可能会造成与其相关联的信息的丢失,请慎重操作!';foreach(array('view','update','publish','offline','delete') as $id){$button=array('label'=>$this->{$id.'ButtonLabel'},'url'=>$this->{$id.'ButtonUrl'},'imageUrl'=>$this->{$id.'ButtonImageUrl'},'options'=>$this->{$id.'ButtonOptions'},);if(isset($this->buttons[$id]))$this->buttons[$id]=array_merge($button,$this->buttons[$id]);else$this->buttons[$id]=$button;}if(!isset($this->buttons['publish']['click'])){$confirmation='';if(Yii::app()->request->enableCsrfValidation){$csrfTokenName = Yii::app()->request->csrfTokenName;$csrfToken = Yii::app()->request->csrfToken;$csrf = "nttdata:{ '$csrfTokenName':'$csrfToken' },";}else$csrf = '';if($this->afterPublish===null)$this->afterPublish='function(){}';$this->buttons['publish']['click']=<<afterPublish;jQuery('#{$this->grid->id}').yiiGridView('update', {type: 'POST',url: jQuery(this).attr('href'),$csrfsuccess: function(data) {jQuery('#{$this->grid->id}').yiiGridView('update');afterPublish(th, true, data);},error: function(XHR) {return afterPublish(th, false, XHR);}});return false;}EOD;}if(!isset($this->buttons['offline']['click'])){if(is_string($this->offlineConfirmation))$confirmation="if(!confirm(".CJavaScript::encode($this->offlineConfirmation).")) return false;";else$confirmation='';if(Yii::app()->request->enableCsrfValidation){$csrfTokenName = Yii::app()->request->csrfTokenName;$csrfToken = Yii::app()->request->csrfToken;$csrf = "nttdata:{ '$csrfTokenName':'$csrfToken' },";}else$csrf = '';if($this->afterOffline===null)$this->afterOffline='function(){}';$this->buttons['offline']['click']=<<afterOffline;jQuery('#{$this->grid->id}').yiiGridView('update', {type: 'POST',url: jQuery(this).attr('href'),$csrfsuccess: function(data) {jQuery('#{$this->grid->id}').yiiGridView('update');afterOffline(th, true, data);},error: function(XHR) {return afterOffline(th, false, XHR);}});return false;}EOD;}if(!isset($this->buttons['delete']['click'])){if(is_string($this->deleteConfirmation))$confirmation="if(!confirm(".CJavaScript::encode($this->deleteConfirmation).")) return false;";else$confirmation='';if(Yii::app()->request->enableCsrfValidation){$csrfTokenName = Yii::app()->request->csrfTokenName;$csrfToken = Yii::app()->request->csrfToken;$csrf = "nttdata:{ '$csrfTokenName':'$csrfToken' },";}else$csrf = '';if($this->afterDelete===null)$this->afterDelete='function(){}';$this->buttons['delete']['click']=<<afterDelete;jQuery('#{$this->grid->id}').yiiGridView('update', {type: 'POST',url: jQuery(this).attr('href'),$csrfsuccess: function(data) {jQuery('#{$this->grid->id}').yiiGridView('update');afterDelete(th, true, data);},error: function(XHR) {return afterDelete(th, false, XHR);}});return false;}EOD;}}}?>

最后要将admin.php中

array(
'class'=>'CButtonColumn',
),

改成:

array(
'class'=>'PButtonColumn',
),

即可。

PHP培训技术

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持28不支持0