Yii CGrid pagination and sorting using CArrayDataProvider not working

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

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

$ campModel = $ model-> function ..

Then I use these lines in CArrayDataProvider:

 $dataProvider=new CArrayDataProvider($campModel); 

Finally, I am trying to view using CGrid:

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

I guess this is due to the way CGrid swap works ... but I'm lost. Thanks for the help :)

+6
source share
3 answers

Create new CSort and CPagination objects and assign them to your datosensor because CArrayDataProvider has not defined them. Here is an example of creating a CSort:

 $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'; 
+9
source

try it,

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

You can also look at CSqlDataProvider. Not sure if this will give you the SQL flexibility you need, but it should handle sorting easier for you than CArrayDataProvider

0
source

Source: https://habr.com/ru/post/917587/


All Articles