Showing posts with label CDataColumn. Show all posts
Showing posts with label CDataColumn. Show all posts

Saturday, June 1, 2013

YII extend the class CGridView CDataColumn

Extend the class CDataColumn

Under protected/components/ create the file DataColumn.php with the following content:

<?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 renderHeaderCell() {
        if($this->evaluateHtmlOptions) {
            foreach($this->headerHtmlOptions as $key=>$value) {
                $options[$key] = $this->evaluateExpression($value,array('row'=>$row,'data'=>$data));
            }
        } else {
            $options=$this->headerHtmlOptions;
        }
        echo CHtml::openTag('td', $options);
        $this->renderHeaderCellContent();
        echo "</th>";
    }
    public function renderFilterCell() {
        if($this->evaluateHtmlOptions) {
            foreach($this->filterHtmlOptions as $key=>$value) {
                $options[$key] = $this->evaluateExpression($value,array('row'=>$row,'data'=>$data));
            }
        } else {
            $options=$this->filterHtmlOptions;
        }
        echo CHtml::openTag('td', $options);
        $this->renderFilterCellContent();
        echo "d</td>";
    }
    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>';
    }
}
?>
Use as following:

<?php 
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    "itemsCssClass" => "table_design_1",
    "htmlOptions" => array(
        "class" => "div_contact_admin_grid_view"
    ),
    "ajaxUpdate" => false,
    'columns'=>array(
        array(
            'class'=>'DataColumn',
            'name'=>'family_name',
            'evaluateHtmlOptions'=>true,
            'htmlOptions'=>array('id'=>'"ordering_{$data->id}"'),
            "cssClassExpression" => "none",
            "filterHtmlOptions" => array("class" => "filterHtmlOptions"),
            "headerHtmlOptions" => array('class' => 'headerHtmlOptions')
        )
    )
)); ?>