如何將CButtonColumn中的Label和imageUrl根據(jù)行數(shù)據(jù)的狀態(tài)的不同展示不同的數(shù)據(jù)呢
例如CGridView的行數(shù)據(jù)中包含鎖定狀態(tài)银择,在已經(jīng)鎖定的狀態(tài)下顯示解鎖圖片,在未鎖定的狀態(tài)下顯示鎖定的狀態(tài)
步驟一: 創(chuàng)建一個CButtonColumn的子類
Yii::import(‘zii.widgets.grid.CButtonColumn’);
class ExprButtonColumn extends CButtonColumn
{
protected function renderButton($id, $button, $row, $data)
{
if (isset($button['label']) && strpos($button['label'], ‘$’) > -1)
{
$button['label']= $this->evaluateExpression($button['label'],array(‘data’=>$data,’row’=>$row));
}
if (isset($button['imageUrl']) && strpos($button['imageUrl'], ‘$’) > -1)
{
$button['imageUrl']= $this->evaluateExpression($button['imageUrl'],array(‘data’=>$data,’row’=>$row));
}
parent::renderButton($id, $button, $row, $data);
}
}
步驟二: 在模板中使用新類進行配置
array(
‘class’=>’ExprButtonColumn’,
‘template’=>’{view}{lock}’,
‘buttons’=>array(
‘view’=>array(
‘label’=>’view project’,
‘url’=>’Yii::app()->controller->createUrl(“project/view”,array(“qid”=>$data->id))’,
),
‘lock’=>array(
‘label’=>’$data->lock_flag == “1″ ? “Unlock The Project” : “Lock The Project”‘,
‘imageUrl’=>’Yii::app()->request->baseUrl.”/images/”.($data->lock_flag == “1″ ? “unlock.gif” : “l(fā)ock.gif”)’,
‘url’=>’Yii::app()->controller->createUrl(“project/lock”,array(“qid”=>$data->id))’,
),
),
),