0

如何使用 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.
4

1 回答 1

2

您需要为 CDbCriteria 和 Sort 类创建对象,并尝试如下使用并根据您的要求更改代码。

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),
    ));
}
于 2014-09-30T11:58:53.800 回答