Yii - CGridView - 添加自己的属性 [英] Yii - CGridView - add own attribute

查看:27
本文介绍了Yii - CGridView - 添加自己的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$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 变量创建属性?

In option value i can add variable from PHP, but for option htmlOptions this is not possible. Why?
How can i make attribute with PHP variable?

推荐答案

当你在 columns 集合没有指定 class 属性,创建的列的类型是 CDataColumn.属性 CDataColumn::value明确记录

When you add arrays in the columns collection without specifying a class property, the type of column being created is CDataColumn. The property CDataColumn::value is explicitly documented to be

一个 PHP 表达式 将为每个数据单元求值并且其结果将呈现为数据单元格的内容.

a PHP expression that will be evaluated for every data cell and whose result will be rendered as the content of the data cells.

因此,value 有一个特殊的属性,它可以获取 eval 为每一行,这就是为什么你可以动态"设置它.然而,这是一个例外,几乎没有其他任何东西支持相同的功能.

Therefore, value has the special property that it gets eval'ed for each row and that's why you can set it "dynamically". This is an exception however, and almost nothing else supports the same functionality.

然而,你很幸运,因为属性 cssClassExpression 是另一个特殊的例外,它正好涵盖了这个用例.所以你可以这样做:

However, you are in luck because the property cssClassExpression is another special exception that covers exactly this usage case. So you can do it like this:

array(
    'name'=>'authorName',
    'value'=>'$data->author->username',
    'cssClassExpression' => '$data->author->username',
),

我在从您的示例中复制/粘贴时犯了一个错误,并且没有注意到您正在尝试对 htmlOptions 中的其他属性执行相同的操作(我现在已经删除了代码的相关部分).

I made a mistake while copy/pasting from your example and did not notice that you were trying to do the same thing for additional attributes inside htmlOptions (I have now deleted the relevant part of the code).

如果您需要为动态值添加更多选项,您别无选择,只能继承 CDataColumn 并覆盖 renderDataCell 方法(股票实现在这里).

If you need to add more options for dynamic values you have no choice but to subclass CDataColumn and override the renderDataCell method (stock implementation is here).

这篇关于Yii - CGridView - 添加自己的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆