Yii CGridView CButtonColumn 根据内容

2017-11-28  本文已影响0人  孤岛渔夫

如何将CButtonColumn中的Label和imageUrl根据行数据的状态的不同展示不同的数据呢

例如CGridView的行数据中包含锁定状态,在已经锁定的状态下显示解锁图片,在未锁定的状态下显示锁定的状态

步骤一: 创建一个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” : “lock.gif”)’,
‘url’=>’Yii::app()->controller->createUrl(“project/lock”,array(“qid”=>$data->id))’,
),
),
),

上一篇下一篇

猜你喜欢

热点阅读