Yii Framework Forum: CLinkColumn and sorting - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CLinkColumn and sorting Rate Topic: -----

#1 User is offline   kire 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 22-October 09

Posted 08 February 2010 - 09:09 AM

Is it possible to have in CGridView a CLinkColumn with sortable header?
0

#2 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 04 March 2010 - 09:30 AM

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',
     )
  )
))

0

#3 User is offline   Sniper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 03-March 10
  • Location:Planet Ruby on Rails

Posted 04 March 2010 - 12:01 PM

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...
3

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users