6

我在模型中构建了一个自定义函数并返回原始数据:

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

$campModel = $model->函数..

然后我在 CArrayDataProvider 中使用这些行:

$dataProvider=new CArrayDataProvider($campModel);

最后我尝试使用 CGrid 查看:

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

我猜这与 CGrid 分页的方式有关......但我迷路了谢谢你的帮助:)

4

3 回答 3

9

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';
于 2012-06-08T13:19:47.377 回答
0

尝试这个,

$this->widget('zii.widgets.grid.CGridView', array(
  'id' => 'bo-campaigns-grid',
  'dataProvider'=> new CArrayDataProvider($campModel, array(
        'pagination' => array(
            'pageSize' => 10
        )
    )),
  ...
于 2012-06-08T07:57:09.567 回答
0

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

于 2012-06-09T04:55:40.640 回答