简体   繁体   English

Yii - CGridView - 添加自己的属性

[英]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',
        ),
    ),
));

In option value i can add variable from PHP, but for option htmlOptions this is not possible. 在选项value我可以从PHP添加变量,但是对于选项htmlOptions这是不可能的。 Why? 为什么?
How can i make attribute with PHP variable? 如何使用PHP变量创建属性?

When you add arrays in the columns collection without specifying a class property, the type of column being created is CDataColumn . columns集合中添加数组而不指定class属性时,正在创建的列的类型是CDataColumn The property CDataColumn::value is explicitly documented to be 属性CDataColumn::value 明确记录

a PHP expression that will be evaluated for every data cell and whose result will be rendered as the content of the data cells. 将为每个数据单元计算的PHP表达式其结果将呈现为数据单元格的内容。

Therefore, value has the special property that it gets eval 'ed for each row and that's why you can set it "dynamically". 因此, value具有为每行进行eval '的特殊属性,这就是为什么你可以“动态”设置它。 This is an exception however, and almost nothing else supports the same functionality. 然而,这是一个例外,几乎没有其他任何东西支持相同的功能。

However, you are in luck because the property cssClassExpression is another special exception that covers exactly this usage case. 但是,你很幸运,因为属性cssClassExpression是另一个特殊的例外,它恰好涵盖了这个用例。 So you can do it like this: 所以你可以这样做:

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

Edit: 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). 编辑:我从你的例子中复制/粘贴时犯了一个错误,并没有注意到你试图对htmlOptions其他属性做同样的事情(我现在已经删除了代码的相关部分)。

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 ). 如果您需要为动态值添加更多选项,您别无选择,只能子类化CDataColumn并覆盖renderDataCell方法( 库存实现在此处 )。

Don't know if this still applies or not (given that there is an accepted answer), but there is a slightly better solution in the form of "rowHtmlOptionsExpression". 不知道这是否仍然适用(假设有一个已接受的答案),但是以“rowHtmlOptionsExpression”的形式有一个稍好的解决方案。 This specifies an expression that will be evaluated for every row. 这指定了将为每一行计算的表达式。 If the result of the eval() call is an array, it will be used as the htmlOptions for the <tr> tag. 如果eval()调用的结果是一个数组,它将被用作<tr>标记的htmlOptions。 So you can basically now use something like this: 所以你现在基本上可以使用这样的东西:

$this->widget('zii.widgets.grid.CGridView', array
(
   ...
   'rowHtmlOptionsExpression' => 'array("id" => $data->id)',
   ...

All your tags will have an id attribute with the records' PK. 你的所有标签都有一个带有记录'PK的id属性。 Just modify the jQuery slightly to obtain the id from that tag instead of an extra column and you should be set. 只需稍微修改jQuery以从该标记获取id而不是额外的列,您应该进行设置。

Extend the class CDataColumn 扩展类CDataColumn

Under protected/components/ create the file DataColumn.php with the following content: 在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>';
    }
}

We can use this new class like this: 我们可以像这样使用这个新类:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM