设为首页收藏本站
查看: 72|回复: 0

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

[复制链接]

论坛元老

Rank: 6Rank: 6

积分
34274
主题
17031
UID
1347
M币
67
贡献
17176

  • 发表于 2014-5-16 09:11:00 | 显示全部楼层 |阅读模式
    因为项目需要,需要给产品添加上架下架的功能,而最方便的方案就是在产品管理界面admin.php下给CButtonColumn组件添加上架和下架的链接操作。
    这里就需要重写CButtonColumn中相关的方法来实现这一功能:
    首先要定义一个继承自CButtonColumn的类PButtonColumn,然后在该类中新增上架和下架的相关属性,经过研究源码,得知在CButtonColumn中新增按钮及添加相应操作的方法主要是initDefaultButtons()方法,重写该方法。
    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 = "\n\t\tdata:{ '$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'),$csrf success: 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 = "\n\t\tdata:{ '$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'),$csrf success: 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 = "\n\t\tdata:{ '$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'),$csrf success: 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',
    ),
    即可。

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    在我站开通SVIP可同时获得17个站点VIP资源 立即登录 立即注册
    快速回复 返回顶部 返回列表