2012-06-28 143 views
0

你好,我需要按ID DESC排序cgridview數據表我怎麼能這樣做?Yii CGridView按ID排序

我的錯誤代碼:

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('name',$this->name,true); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
      'sort'=>array(
        'ID'=>array(
         'desc'=>'ID DESC', 
        ), 
      ), 
     )); 
    } 

回答

3

試試接近:

$criteria=new CDbCriteria; 

$criteria->compare('ID',$this->ID); 
$criteria->compare('name',$this->name,true); 
$criteria->order = 'ID DESC'; 


$dataProvider=new CActiveDataProvider('Nameofclass', array(
    'criteria'=>$criteria 
)); 
1

您還可以使用defaultSort默認

$criteria=new CDbCriteria; 

$criteria->compare('ID',$this->ID); 
$criteria->compare('name',$this->name,true); 

$dataProvider=new CActiveDataProvider('Nameofclass', array(
    'criteria'=>$criteria, 
    'sort'=>array(
     'defaultOrder'=>'ID DESC', 
    ), 
)); 
2

來設置它在你的類添加此行在您的搜索()下以id降序顯示記錄。

$ criteria-> order ='ID DESC';

0
$criteria = new CDbCriteria; 
    $criteria->compare('id', $this->id); 
    $criteria->compare('date', $this->date, true); 
    $criteria->compare('last', $this->last, true); 
    $criteria->compare('first', $this->first, true); 
    //$criteria->order = 'id DESC'; // thats not works, if you use CSORT 

    return new CActiveDataProvider($this, array(
     'criteria' => $criteria, 
     'pagination' => array(
      'pageSize' => 20 
     ), 
     'sort' => array(
       'attributes' => array(
        'id' => array("asc" => 'id', "desc" => 'id desc'), 
        'last' => array("asc" => 'last', "desc" => 'last desc'), 
        'first' => array("asc" => 'first', "desc" => 'first desc'), 
        'date' => array("asc" => 'date', "desc" => 'date desc'), 
       ),//attributes array 
       'defaultOrder' => 'id DESC',//thats the clue, defaultOrder is on the sort array 
     ),//sort array 
    ));