CGridview custom field sortable

2019-07-09 09:19发布

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?

标签: yii cgridview
2条回答
ら.Afraid
2楼-- · 2019-07-09 09:58

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

Using standard filters in CGridView custom fields

查看更多
家丑人穷心不美
3楼-- · 2019-07-09 10:05

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.

查看更多
登录 后发表回答