如何在Yii中的GRIDVIEW中为每个ROW添加TITLE标记

时间:2013-07-02 04:31:44

标签: gridview yii cgridview

我想将标题属性添加到Gridview中的每一行。 有什么方法可以让这件事发挥作用。

使用=>

可以实现添加类
$rowCssClassExpression = '$data->id';

在网格行Gridview中添加标题属性> TR>标题?

<table class="items">
<tbody>
<tr class="odd" title="**I need to put title dynamically to each row**">
<td style="width:18%;line-height:2em;">
<td style="width:22%;">Jay </td>
<td style="width:15%;">Sonet Systems</td>
<td style="width:10%;">98012269</td>
<td style="width:15%;">Moderate Risk</td>
<td style="width:20%;">Suicide Call Back Service</td>
</tr>
<tr class="even">
<td style="width:18%;line-height:2em;">
<td style="width:22%;"> </td>
<td style="width:15%;">Susan Rosenthal</td>
<td style="width:10%;"> </td>
<td style="width:15%;">Moderate Risk</td>
<td style="width:20%;">Suicide Line</td>
</tr>

1 个答案:

答案 0 :(得分:2)

您必须覆盖班级CDataColumn

下面是一个如何操作的示例,我从Yii Website获得了此代码。

/**
 * 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(
            'class'=>'DataColumn',
            'name'=>'name',
            'evaluateHtmlOptions'=>true,
            'htmlOptions'=>array('title'=>'{$data->name}'),
        ),
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));