Yii CGridView按ID排序

时间:2012-06-28 18:25:32

标签: php yii

你好我需要通过id DESC对cgridview数据表进行排序我怎么能这样做?

我的错误代码:

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',
                    ),
            ),
        ));
    }

4 个答案:

答案 0 :(得分: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 :(得分:2)

在您的班级中在您的搜索()下添加此行,以按ID降序显示记录。

$ criteria-> order =' ID DESC&#39 ;;

答案 2 :(得分: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',
    ),
));

答案 3 :(得分: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
      ));