Yii CGrid pagination and sorting with CArrayDataProvider doesnt work

不打扰是莪最后的温柔 提交于 2019-12-09 13:01:48

问题


I built a custom function in my model and return the raw data:

function(){
...
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$rows=$command->queryAll();
return $rows;
}

$campModel = $model->function..

I then use those rows in CArrayDataProvider:

$dataProvider=new CArrayDataProvider($campModel);

Finally i'm trying to view using CGrid:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bo-campaigns-grid',
'dataProvider'=>$campModel,...

I'm guessing this has to do with the way CGrid is paging...but i'm lost Thanks for the help :)


回答1:


Create a new CSort and CPagination objects and assign them to your dataprovider, because CArrayDataProvider doesn't have them defined. Here is an example of CSort creation:

$dataProvider=new CArrayDataProvider($campModel);
$sort = new CSort();
$sort->attributes = array(
            'fecha'=>array(
                'asc'=>'dateA DESC',
                'desc'=>'dateA ASC',
            ),
);
$sort->route = 'myController/myMethod';
$dataProvider->sort = $sort;
$dataProvider->sort->defaultOrder='dateA DESC';



回答2:


try this,

$this->widget('zii.widgets.grid.CGridView', array(
  'id' => 'bo-campaigns-grid',
  'dataProvider'=> new CArrayDataProvider($campModel, array(
        'pagination' => array(
            'pageSize' => 10
        )
    )),
  ...



回答3:


You could also look at CSqlDataProvider. Not sure if it would give you the SQL flexibility you need, but it should handle sorting for you more easily than CArrayDataProvider



来源:https://stackoverflow.com/questions/10944119/yii-cgrid-pagination-and-sorting-with-carraydataprovider-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!