![](/img/trans.png)
[英]Yii add filter to a virtual attribute in CGridView and make it sortable
[英]Yii - CGridView - add own attribute
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'title', // display the 'title' attribute
'category.name', // display the 'name' attribute of the 'category' relation
'content:html', // display the 'content' attribute as purified HTML
array( // display 'create_time' using an expression
'name'=>'create_time',
'value'=>'date("M j, Y", $data->create_time)',
),
array( // display 'author.username' using an expression
'name'=>'authorName',
'value'=>'$data->author->username',
//HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'htmlOptions'=>array('class'=>'$data->author->username', 'secondAttribute' => $data->author->id),
//HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
),
array( // display a column with "view", "update" and "delete" buttons
'class'=>'CButtonColumn',
),
),
));
在选项value
我可以从PHP添加变量,但是对于选项htmlOptions
这是不可能的。 为什么?
如何使用PHP变量创建属性?
在columns
集合中添加数组而不指定class
属性时,正在创建的列的类型是CDataColumn
。 属性CDataColumn::value
明确记录为
将为每个数据单元计算的PHP表达式,其结果将呈现为数据单元格的内容。
因此, value
具有为每行进行eval
'的特殊属性,这就是为什么你可以“动态”设置它。 然而,这是一个例外,几乎没有其他任何东西支持相同的功能。
但是,你很幸运,因为属性cssClassExpression
是另一个特殊的例外,它恰好涵盖了这个用例。 所以你可以这样做:
array(
'name'=>'authorName',
'value'=>'$data->author->username',
'cssClassExpression' => '$data->author->username',
),
编辑:我从你的例子中复制/粘贴时犯了一个错误,并没有注意到你试图对htmlOptions
其他属性做同样的事情(我现在已经删除了代码的相关部分)。
如果您需要为动态值添加更多选项,您别无选择,只能子类化CDataColumn
并覆盖renderDataCell
方法( 库存实现在此处 )。
不知道这是否仍然适用(假设有一个已接受的答案),但是以“rowHtmlOptionsExpression”的形式有一个稍好的解决方案。 这指定了将为每一行计算的表达式。 如果eval()调用的结果是一个数组,它将被用作<tr>标记的htmlOptions。 所以你现在基本上可以使用这样的东西:
$this->widget('zii.widgets.grid.CGridView', array
(
...
'rowHtmlOptionsExpression' => 'array("id" => $data->id)',
...
你的所有标签都有一个带有记录'PK的id属性。 只需稍微修改jQuery以从该标记获取id而不是额外的列,您应该进行设置。
扩展类CDataColumn
在protected / components /下,使用以下内容创建DataColumn.php文件:
/**
* DataColumn class file.
* Extends {@link CDataColumn}
*/
class DataColumn extends CDataColumn
{
/**
* @var boolean whether the htmlOptions values should be evaluated.
*/
public $evaluateHtmlOptions = false;
/**
* Renders a data cell.
* @param integer $row the row number (zero-based)
* Overrides the method 'renderDataCell()' of the abstract class CGridColumn
*/
public function renderDataCell($row)
{
$data=$this->grid->dataProvider->data[$row];
if($this->evaluateHtmlOptions) {
foreach($this->htmlOptions as $key=>$value) {
$options[$key] = $this->evaluateExpression($value,array('row'=>$row,'data'=>$data));
}
}
else $options=$this->htmlOptions;
if($this->cssClassExpression!==null)
{
$class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
if(isset($options['class']))
$options['class'].=' '.$class;
else
$options['class']=$class;
}
echo CHtml::openTag('td',$options);
$this->renderDataCellContent($row,$data);
echo '</td>';
}
}
我们可以像这样使用这个新类:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'article-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'title',
array(
'name' => 'author',
'value' => '$data->author->username'
),
array(
'class' => 'DataColumn',
'name' => 'sortOrder',
'evaluateHtmlOptions' => true,
'htmlOptions' => array('id' => '"ordering_{$data->id}"'),
),
array(
'class' => 'CButtonColumn',
),
),
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.