Pagination Problem In Cgridview

Hello Experts,

Are you familiar with the pagination function of yii or more specific the pagination in the CActiveDataProvider?

There seems to be a problem with the synchronization of the pagenumbers with the actual data.

I use a web form on the index page of the site to search for specific data in a database. If I submit this form, a table with the searched information get displayed in a table (zii.widgets.grid.CGridView) located under the form. The data is shown correctly on page 1. But if I click the link to page 2, I get a second page which contains not only the searched data, but all data from the underlying table.

This is my data provider:


$dataProvider=new CActiveDataProvider('View_Suppression_Hist', array(

              'sort'=>array(

                  'attributes'=>array(

                       'account_id', 'suppression_list_dt',

                  ),

              ),

                'pagination'=>array(

                    'pageSize' => 10,                    

                ),

          ));

And here is the controller code that asks for the data:


public function actionIndex()

	{

               $model=new View_Suppression_Hist;

               $dataProvider = $this->getSourcesTable();

               

               if(isset($_POST['View_Suppression_Hist'])) 

               { 

                 $model->attributes=$_POST['View_Suppression_Hist']; 

                 if($model->validate()) 

                 { 

                    $dataProvider = $model->search();                           

                 } 

               } 

               

               $this->render('index',array(

                   'model'=>$model,

                   'dataProvider' => $dataProvider

                   )

               );                


	}

The view widget looks as follows:


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'enablePagination'=>true

));

I want to achieve that the second page just shows up the rest of the searched data and not everything. Maybe the criteria is getting lost between multiple page loads?

Thanks in advance for your support.

Best Regards,

Moe

Hey Guys,

I resolved the problem by doing this:

In the model file (My_Model.php):


	public function search()

	{

                $criteria=new CDbCriteria;


		$criteria->compare('id',$this->id,true);

                //... further criteria defined here

 

                // save the criteria in a session variable

                Yii::app()->session['sql_criteria'] = $criteria;

                

                return $this->getDataProvider();

	}


        /**

            return the data provider with the criteria saved in the current session

        */

        public function getDataProvider()

        {

            return new EActiveDataProviderEx($this, array(

		    'criteria'=>Yii::app()->session['sql_criteria'],

                    'cache'   =>array(3600),

                    'sort'=>array(

                      'attributes'=>array(

                           'account_id', 'suppression_list_dt',

                      ),

                  ),

                  'pagination'=>array(

                        'pageSize'=>20, // or another reasonable high value...

                    ),

		));

        }

In my controller I used the following code:


	public function actionIndex()

	{

               $model=new My_Model;

               if(isset($_POST['My_Model'])) 

               { 

                 $model->attributes=$_POST['My_Model']; 

                 if($model->validate()) 

                 { 

                    $dataProvider = $model->search();                                               

                 } 

               } 

               else

               {

                   $dataProvider = $model->getDataProvider();

               }

               

               $this->render('index',array(

                   'model'=>$model,

                   'dataProvider' => $dataProvider

                   )

               );                

	}

The important thing is the session variable which is used to hold the criteria over a search. If the search is triggered by the form submit, the session variable is set (or modified if it already exists). If we use the data provider at a later time, we are able to get the criteria out of this session variable and use it for further search.

Don’t be confused that I use “EActiveDataProviderEx”. This is just a yii extension to provide a simple way to cache the data from previous searches.

Hope that will help some of you who have the same pagination problem. Maybe there is a better way to solve this problem. If so, don’t hesitate to post your alternative solution.