Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add TITLE tag for each ROW in a GRIDVIEW in Yii

I want to add title attributes to each and every row in a Gridview. Is there any way I can get this thing working.

Adding a Class can be achieved by using =>

$rowCssClassExpression = '$data->id';

Adding Title Attribute In A Grid Row 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>
like image 886
Shareek Ahamed Avatar asked Jan 30 '26 16:01

Shareek Ahamed


1 Answers

You have to override the class CDataColumn.

Below an example of how to do it, I got this code from 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>';
    }
}

How to use his:

$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',
        ),
    ),
));
like image 84
Eduardo M Avatar answered Feb 03 '26 10:02

Eduardo M