Wednesday, June 06, 2012

YII: How to Sort or Reverse CGridView Display Order

Here is how you can sort or reverse the order of CGridView (showing the newest on top by default).

In the Controller (/protected/controllers/postController.php)

public function actionAdmin() {    $model = new Post('search');    $model->unsetAttributes();    if (isset($_GET['Post']))        $model->attributes = $_GET['Post'];
    $this->render('admin', array(        'model' => $model,    ));

In the Model (/protected/models/post.php)

public function search()        {                // Warning: Please modify the following code to remove attributes that                // should not be searched.
                $criteria=new CDbCriteria;
                $criteria->compare('id',$this->id);                $criteria->compare('foo',$this->foo,true);                $criteria->compare('bar',$this->bar,true);                ....
                return new CActiveDataProvider($this, array(                        'criteria' => $criteria,                        'sort' => array(                             'defaultOrder' => 'post_date DESC',  // this is it.                        ),                        'pagination' => array(                                'pageSize' => 30,                        ),                ));        }
 Thank softark for the answer!!
http://www.yiiframework.com/forum/index.php/topic/21523-yii-reverse-cgridview-display-order/

No comments: