Sortable CGridView column with links

If you use CLinkColumn to render links in any column then you loose an ability to sort entire CGridView by that link-containing column.

To work-around this problem render link in your cells using your own function and do not use CLinkColumn.

So, instead of doing something like this:

array
(
    'class'=>'CLinkColumn',
    'labelExpression'=>'$data->number',
    'urlExpression'=>'"http://some-url.com/emp/".$data->number',
    'htmlOptions'=>array('style'=>'width: 70px')
)

Use column definition similar to this:

array
(
    'type'=>'raw',
    'name'=>'number',
    'value'=>'gridNumber($data)',
    'htmlOptions'=>array('style'=>'width: 70px')
)

And define gridNumber() function:

function gridNumber($model)
{
    return '<a href="http://some-url.com/emp/'.$model->number.'">'.$model->number.'</a>';
}

You will end up with pretty links in your cells, exactly the same, as in case of using CLinkColumn. And you still will be able to sort entire CGridView by that column.

Leave a Reply