3

我想为 Gridview 中的每一行添加标题属性。有什么办法可以让这件事正常工作。

添加一个类可以通过使用 =>

$rowCssClassExpression = '$data->id';

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

<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>
4

1 回答 1

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',
        ),
    ),
));
于 2013-07-02T04:59:55.203 回答