2014-09-30 63 views
0

如何使用CSqldataprovider對CGridView列進行排序(單擊列標題時)。如何使用CSqldataprovider對CGridView列進行排序?

在控制器

$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, 
      ), 
    )); 

鑑於

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

這是給錯誤

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

回答

2

你需要爲CDbCriteria創建對象和排序類,並嘗試如下使用,更改您的要求的代碼。

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), 
    )); 
} 
相關問題