Monday, August 11, 2014

How to Set Default Sorting Order in Yii CGridView Model

Here it is a simple solution to set default sort order in Yii CGridView. This will help you to easily sort items in a grid view with just a single line of code in model search. Here we are sorting it using create_dt in Ascending order
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'created_dt DESC',
),
));
And model search function will look like this.
public function search()
 {
 $criteria=new CDbCriteria;
 $criteria->compare('id',$this->id,true);
 $criteria->compare('user_id',$this->user_id);
 $criteria->compare('title',$this->title,true);
 $criteria->compare('heading',$this->heading,true);
 $criteria->compare('slug',$this->slug,true);
 $criteria->compare('description',$this->description,true);
 $criteria->compare('post_type',$this->post_type);
 $criteria->compare('meta_index',$this->meta_index);
 $criteria->compare('created_dt',$this->created_dt,true);
 $criteria->compare('image',$this->image,true);
 $criteria->compare('status',$this->status,true);
 $criteria->compare('cat_id',$this->cat_id);

 return new CActiveDataProvider($this, array(
  'criteria'=>$criteria,
  'sort'=>array(
                  'defaultOrder'=>'created_dt DESC',
                    ),
 ));
}
No comments :

No comments :

Post a Comment