Yii 1.1: Adding a link to a CDataColumn

9 followers

Backoffice Grid Views often list information like Posts for a blog while showing at the same time linked information like the User who wrote that post. For more efficiency, it is appropriate that the User is displayed as a link to the User detail page.

CDataColumns allow adding a link by modifying the 'value' property for the column, whicle CLinkColumns are built to create columns with links, but not really associated to data.

In order to make the addition of links as easy as this:

$this->widget('zii.widgets.grid.CGridView', array(
    [...other gridview options...],
    'columns'=>array(
                  array(
                     'name'=>'username',
                     'urlExpression'=>'array("admin/devices/view","id"=>$data->user_id)',
                     'class'=>'YDataLinkColumn',
                  ),
               )
    ));

, I created the following class (already referenced in the example above):

/**
 * YDataLinkColumn extends {@link CDataColumn} to facilitate adding
 * links to data values.
 *
 * This is particularly usefull in the backend to go from one entity
 * to the other.
 *
 * YDataLinkColumn 'joins' the {@link CLinkColumn} and {@link CDataColumn}
 * interfaces.
 */
class YDataLinkColumn extends CDataColumn {
 
    public $urlExpression;
    public $url="javascript:void(0)";
    public $linkHtmlOptions=array();
    public $imageUrl;
 
    protected function renderDataCellContent($row, $data) {
        ob_start();
        parent::renderDataCellContent($row, $data);
        $label = ob_get_clean();
 
        if($this->urlExpression!==null)
            $url=$this->evaluateExpression($this->urlExpression,array('data'=>$data,'row'=>$row));
        else
            $url=$this->url;
 
        $options=$this->linkHtmlOptions;
        if(is_string($this->imageUrl))
            echo CHtml::link(CHtml::image($this->imageUrl,$label),$url,$options);
        else
            echo CHtml::link($label,$url,$options);
    }
}

Total 6 comments

#15888 report it
le_top at 2013/12/26 09:06pm
Why not CLinkColumn?

Just saw St Clair's post and wondered why I did not use CLinkColumn just like that.

As far as I can see (in a short glimpse), I conclude that CLinkColumn does not provide filtering functionnality on the data while CDataColumn does. So if you want to sort the data represented in the links, CLinkColumn alone will not do the job.

#14021 report it
Albert St Clair at 2013/07/12 07:48am
Using CLinkColumn to do this

This is cool here is how I did it using CLinkColumn....

$this->widget('zii.widgets.grid.CGridView', array(
 [...other gridview options...],
            'columns'=>array( 
                    array(    
                    'header'=>'username',
                    'labelExpression'=>'$data->username',
                    'urlExpression'=>'array("admin/devices/view","id"=>$data->id)',
                    'class'=>'CLinkColumn'         
                ),
          ),
));
#13308 report it
ruderbytes at 2013/05/22 02:25am
Thanks

thanks for code share :)

#12831 report it
Lal Zada at 2013/04/15 03:44am
THANKS

very useful

#12308 report it
le_top at 2013/03/12 05:37pm
PcLinkButton

I didn't see pclinkbutton before - it adds the imageUrlExpression function. You are the author so you noticed the ressemblance.

What I prefer in my proposal is that it is easy to move from a CDataColumn (which is the default class) to YDataLinkColumn (without the need to change to labelExpression) and that CDataColumn is overloaded rather than copied.

Anyway, the idea is to share thoughts ;-).

#12307 report it
Boaz at 2013/03/12 05:13pm
Another relevant extension

http://www.yiiframework.com/extension/pclinkbutton/

Leave a comment

Please to leave your comment.

Write new article