这是一个按钮的代码,我想改变标签或者换句话说标题标签的内容。
'assignParent' => array(
'label' => 'Assign Parent',
'url' => '$data->parentId ? Yii::app()->controller->createUrl("updateParent", array("id" => $data->parentId)): Yii::app()->controller->createUrl("assignParent", array("imei" => $data->imei))',
'imageUrl' => Yii::app()->baseUrl . '/media/images/parent-btn.png',
'visible' => 'Yii::app()->user->checkAccess("oDeviceDeviceAssignParent") ? true : false',
'options' => array('style' => 'padding: 0px 3%'),
),
这是视图源代码,
<td class="button-column"><a href="/qelasysecurity_12/index.php/device/device/view/id/18" rel="tooltip" title="View" style="padding: 0px 3%"><i class="icon-eye-open"></i></a><a href="/qelasysecurity_12/index.php/device/device/update/id/18" rel="tooltip" title="Update" style="padding: 0px 3%"><i class="icon-pencil"></i></a><a href="/qelasysecurity_12/index.php/device/device/delete/id/18" rel="tooltip" title="Delete" class="delete" style="padding: 0px 3%"><i class="icon-trash"></i></a><a href="/qelasysecurity_12/index.php/device/device/updateParent/id/36" rel="tooltip" title="Assign Parent" style="padding: 0px 3%"><img alt="Assign Parent" src="/qelasysecurity_12/media/images/parent-btn.png"></a><a href="#" rel="tooltip" title="$data->parentId ? Assign Student : Update Student" style="padding: 0px 3%"><img alt="Assign Student" src="/qelasysecurity_12/media/images/student-btn.png"></a></td>
这就是那部分的由来,
<a href="#" rel="tooltip" title="$data->parentId ? Assign Student : Update Student" style="padding: 0px 3%">
我想做这样一个逻辑来改变标签的名称,
'options' => array('style' => 'padding: 0px 3%', 'title'=>'$data->parentId ? Assign Student : Update Student'),
数据-> parentId吗?Assign Student: Update Student'
有什么建议吗?
解决方案如下:通过http://www.yiiframework.com/wiki/714/yii-1-1-cgridview-use-special-variable-data-in-the-options-of-a-button-i-e-evaluate-options-attribute/
的帮助在/protected/components中创建一个新的扩展名"ButtonColumn.php"
<?php
/**
* ButtonColumn class file.
* Extends {@link CButtonColumn}
*/
class ButtonColumn extends CButtonColumn
{
/**
* Renders a link button.
* @param string $id the ID of the button
* @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
* See {@link buttons} for more details.
* @param integer $row the row number (zero-based)
* @param mixed $data the data object associated with the row
*/
protected function renderButton($id,$button,$row,$data)
{
if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
return;
$label=isset($button['label']) ? $button['label'] : $id;
$url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
$options=isset($button['options']) ? $button['options'] : array();
if(!isset($options['title']))
$options['title']=$label;
// Start of modification
if( isset ( $button['evaluateLabel'] ) )
{
$label = $this->evaluateExpression($label,array('data'=>$data,'row'=>$row));
$label = $button['evaluateLabel'][$label];
unset($options['evaluateLabel']);
}
// END of modifications
if(isset($button['imageUrl']) && is_string($button['imageUrl']))
echo CHtml::link(CHtml::image($button['imageUrl'],$label),$url,$options);
else
echo CHtml::link($label,$url,$options);
}
}
注意修改行。这将检查一个新的变量'evaluateLabel'。如果它退出,它将通过数组键解析'label'变量以查找所需的新值。
我的新CGridView代码如下所示
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'username',
'email',
...
array(
'class' => 'ButtonColumn',
'template' => '{update} {delete} {switch}',
'buttons'=>array (
'delete'=>array(
'label'=>'$data->custom_variable',
'imageUrl'=>false,
'options'=>array( 'class'=>'cbutton delete' ),
'evaluateLabel' => array(0=>"Suspend",1=>"Reactivate"),
),
所以在我的情况下'$data->custom_variable是绑定到模型的变量,它将始终为0或1。因此,加载的文本将在evaluateLabel键中进行配对。
还要注意类已经从CButtonColumn变成了ButtonColumn
我认为你不能直接这样做,但你可以使用属性cssClassExpression
你可以在里面写一个有效的PHP表达式来计算。创建两个css类,一个内容为assign student,另一个内容为update student。你可以这样写,
1. 在模型checkStudent
中编写一个函数,如
public function checkStudent()
{
if($this->id)
{
return "class name"
}
else
{
return "class name"}
}
2.现在你可以像这样使用
"cssClassExpression"=>'$data->checkStudent()'
现在问题来了,我们如何使用Css类正确的内容。
你可以使用我的代码
'link'=>array(
'header'=>Yii::t('main', 'login'),
'type'=>'raw',
'value'=> 'CHtml::button($data->islogin==1?"可":"不可",array("onclick"=>"document.location.href=''".Yii::app()->controller->createUrl("user/changelogin",array("id"=>$data->id))."''", "class"=>"btn-link"))',
),