6

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 :)

tereško
  • 58,060
  • 25
  • 98
  • 150
Danny Valariola
  • 1,118
  • 5
  • 26
  • 41

3 Answers3

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';
sucotronic
  • 1,504
  • 9
  • 20
  • Thanks... I think this nailed it :) – Danny Valariola Jun 09 '12 at 05:54
  • Thanks so much for the helpful suggestion. Here's a question: Is it possible to have the sort action reload the grid with ajax instead of reloading the entire page? – bjtilley Jul 01 '13 at 13:40
  • @bjtilley by default CGridView uses ajax updates, unless it detects that javascript is disabled. See reference: http://www.yiiframework.com/doc/api/1.1/CGridView – sucotronic Jul 02 '13 at 10:25
0

try this,

$this->widget('zii.widgets.grid.CGridView', array(
  'id' => 'bo-campaigns-grid',
  'dataProvider'=> new CArrayDataProvider($campModel, array(
        'pagination' => array(
            'pageSize' => 10
        )
    )),
  ...
Alex
  • 2,102
  • 1
  • 13
  • 16
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

acorncom
  • 5,975
  • 1
  • 19
  • 31