2

I had created with custom field in yii Cgridview but how to make that sortable. The custom field value is from a function in the model. I want to make this field sortable? Can someone help me?

Kai
  • 38,985
  • 14
  • 88
  • 103
Karthikeyan
  • 69
  • 1
  • 3
  • 4
  • 1
    We need a bit more info - did you define a custom sort? What data provider are you using? – ldg Oct 03 '11 at 18:49

2 Answers2

4

In the search function of your model, where customField is the name of your field:

// ...other criteria...
$criteria->compare('customField',$this->customField);

$sort = new CSort();
$sort->attributes = array(
    'customField'=>array(
        'asc'=>'customField ASC',
        'desc'=>'customField DESC',
    ),
    '*', // this adds all of the other columns as sortable
);

return new CActiveDataProvider($this, array(
    'criteria'=>$criteria,
    'sort'=>$sort,
));

You may also need to update rules and attributeLabels in your model to reflect the new custom field.

bweaver
  • 143
  • 1
  • 12
  • can you describe what kind of updates are required for **rules** and **attributeLabels** in model to reflect new custom field. more details please – Darshit Gajjar Jan 03 '12 at 11:59
0

There is a detailed description about searching on custom fields at this URL:

Using standard filters in CGridView custom fields

Jay Haase
  • 1,979
  • 1
  • 16
  • 36