dongyi2993 2014-09-30 10:54
浏览 17
已采纳

如何使用CSqldataprovider使CGridView列可排序?

How to make CGridView columns sortable (On clicking column title) using CSqldataprovider.

In controller

    $sql = "select id ,name, address
            from User
            where city = 'ABC' ";
    $rawData = Yii::app()->db->createCommand($sql);
    return $allMovies = new CSqlDataProvider($rawData, array(
        'keyField' => 'id',
        'sort'=>array(
            'attributes'=>array(
            'id', 'name', 'address',
          ),
        ),
        'pagination' => array(
                'pageSize' => 10,
            ),
    ));

In view

 $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'all_movies',
    'dataProvider' => $allMoviesStats,
    'columns' => array(
            'id',
            'name', 
            'address',
            'city'
      )
    )
);?>

It is giving error

 "error":{"code":99,"text":"Property \"CGridView.sort\" is not defined.
  • 写回答

1条回答 默认 最新

  • duanqiang2617 2014-09-30 11:58
    关注

    You need to create object for CDbCriteria and Sort class and try to use as below and change the code for your requirement.

    public function search()
    {
        $criteria=new CDbCriteria;
        $criteria->condition="active=1";
        if($this->name=="Enter Country Name" || $this->name=='') {
            $this->name='';
        } else {
            $this->name=$this->name;
        }
        $criteria->compare('name',$this->name,true);
        $sort = new CSort;
        $sort->defaultOrder = 'id DESC';
        $sort->attributes = array(          
            'name' => array(
                'asc'   =>'name',
                'desc'  =>'name DESC',
            ),
            ...
            ... // attributes to sort
        );
        return new CActiveDataProvider('Country', array( //Country is nothing but you model class name
            'criteria'  =>$criteria,
            'sort'       => $sort,
            'pagination'=>array('pageSize'=> 10),
        ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度