CGridview custom field sortable

2019-07-09 10:01发布

问题:

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?

回答1:

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.



回答2:

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

Using standard filters in CGridView custom fields



标签: yii cgridview