作者:HAOCWH | 来源:互联网 | 2014-05-16 09:11
因为项目需要,需要给产品添加上架下架的功能,而最方便的方案就是在产品管理界面admin.php下给CButtonColumn组件添加上架和下架的链接操作。
这里就需要重写CButtonColumn中相关的方法来实现这一功能:
首先要定义一个继承自CButtonColumn的类PButtonColumn,然后在该类中新增上架和下架的相关属性,经过研究源码,得知在CButtonColumn中新增按钮及添加相应操作的方法主要是initDefaultButtons()方法,重写该方法。
Yii::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 = "\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',
),
即可。