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 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下