Is it possible to have in CGridView a CLinkColumn with sortable header?
Is it possible to have in CGridView a CLinkColumn with sortable header?
Hi there
I have it working with the latest yii (non-stable but might work with the current stable release). (could do with some improvement but it does what I need)
try this; create a new component under components/SCLinkColumnWithSort.php
class SCLinkColumnWithSort extends CLinkColumn
{
public $name;
public $sortable = true;
public $filter;
protected function renderFilterCellContent()
{
$this->linkHtmlOptions['class'] = $this->name;
if($this->filter!==false && $this->grid->filter!==null && strpos($this->name,'.')===false)
{
if(is_array($this->filter))
echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
else if($this->filter===null)
echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
else
echo $this->filter;
}
else
parent::renderFilterCellContent();
}
protected function renderHeaderCellContent()
{
if($this->grid->enableSorting && $this->sortable && $this->name!==null)
echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);
else
parent::renderHeaderCellContent();
}
}
then with the gridview widget call
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'class' => 'SCLinkColumnWithSort',
'name' => 'column_name_to_sort',
)
)
))
alternatively you can use (probably much better)
array(
'name' => 'field_name',
'type' => 'raw',
'value' => 'CHtml::link($data->field_name,$data->field_name)'
),
I’ve removed some code from my above post, as I’ve added some ajax related stuff…