[Solved] Custom Page Size For Cgridview - Error

Hi…

Ive been trying to implement the Custom Page Size for CGridView that is mentioned in the url : http://www.yiiframework.com/forum/index.php/topic/8994-dropdown-for-pagesize-in-cgridview/

But for me it gives another error :




TypeError: $.param.querystring is not a function

[Break On This Error] 	


options.url = $.param.querystring(options.url, options.data);



My Controller :




//Notifications

	public function actionNotifications() {

		

		$model = new Notifications;


		//

		// page size drop down changed

		if (isset($_GET['pageSize'])) {

		//

		// pageSize will be set on user's state

		Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);

		//

		// unset the parameter as it

		// would interfere with pager

		// and repetitive page size change

		unset($_GET['pageSize']);

		}

				

		$key = Yii::app()->request->getQuery('key');

        if($key) {

            $model->notificationText = $key;

        }

		

        $this -> layout = 'main';

        $this->render('notifications' ,array('model' => $model));			

		

	}



My model :




/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'mobsrv_notification';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('notificationText', 'required'),

			array('createdDate, modifiedDate', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('notificationText', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'notificationText' 	=> 'Notification Title',

			'createdDate' 		=> 'Notification Created Date',

			'modifiedDate' 		=> 'Notification Modified Date'

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


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

		

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

	  public function notificationList() {

            $criteria=new CDbCriteria;

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

            $sort = new CSort;

			$sort->attributes = array('*');

			$pagination = new CPagination;

	

			if ($this->notificationText) {

				

				$sort->params = array(

					'key' => $this->notificationText,

					'page' => Yii::app()->request->getQuery('page', '0'),

				);       

				

				$pagination->params = array(

					'key' => $this->notificationText,

					'sort' => Yii::app()->request->getQuery('sort', 'notificationId'),

					//'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize'])

				);

			}

			$sort->defaultOrder = 'notificationId';

			


			return new CActiveDataProvider(get_class($this),array(

			'pagination'=>array(

				//

				// please check how we get the

				// the pageSize from user's state

				'pageSize'=> Yii::app()->user->getState('pageSize',

				//

				// we have previously set defaultPageSize

				// on the params section of our main.php config file

				Yii::app()->params['defaultPageSize']),

			),

			'sort' => $sort,

			'criteria'=>$criteria,

			));




   

        }



My View Part :




<!--listing holder start-->

            <?php 

              

            $pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']);

            // we use header of button column for the drop down   


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

                            'id' => 'notifications-grid',

                            'cssFile' => Yii::app()->baseUrl . '/css/gridview.css',

                            'dataProvider' => $model->notificationList(),

                            'enablePagination' => true,

                            'ajaxUpdate' => false,

                            'emptyText' => 'No results found.',

                            'template' => '{items}',

                            'pager' => array(

                                'class' => 'LinkPager',

                                'cssFile' => false,

                                'header' => '',

                                'firstPageLabel' => '',

                                'prevPageLabel' => '<img  class="next-btn">',

                                'nextPageLabel' => '<img  class="previous-btn">',

                                'lastPageLabel' => '',

                            ),

                            'columns' => array(

                                array(

                                        'name' => 'id',

                                        'type' => 'raw',

                                        'htmlOptions' => array('class' => 'width40'),

                                        'headerHtmlOptions'=>array('class' => 'width40'),

                                        'header' => 'S/N',

                                        'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',

                                    ),

                                array(

                                    'name' => 'notificationText',

                                    'type' => 'raw',

                                    'htmlOptions'=> array('class'=>'width400'),

                                    'headerHtmlOptions'=>array('class' => 'width400'),

                                    'header' => 'Notification Text',

                                    'value' => '$data->notificationText',

                                ),                                

                                array(

                                    'name' => 'createdDate',

                                    'type' => 'raw',

                                    'htmlOptions'=> array('class'=>'width150'),

                                    'header' => 'Created On',

                                    'value' => '$data->createdDate',

                                ), 

                                array(

                                    'class' => 'CButtonColumn',

                                    'header'=>CHtml::dropDownList('pageSize',

                                        $pageSize,

                                        array(20=>20,50=>50,100=>100),

                                        array(   

                                       // change 'user-grid' to the actual id of your grid!!

                                        'onchange'=>

                                        "$.fn.yiiGridView.update('notifications-grid',{ data:{pageSize: $(this).val() }})",

                                    )),

                                    'htmlOptions' => array('class' => 'width110'),

                                    'headerHtmlOptions'=>array('class' => 'width110'),

                                    'template' => '{update} {delete}  <a class="file-btn" href="/isml/source/portal/index.php/user/editnotification" title="Click here to copy notification"></a>',

                                    'buttons' => array()                                    

                                    ),

                                

                           


                            ),));

               ?>



My page source for the header would look like this(there are no scripts added anywhere else) :




<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" type="text/css" href="/isml/source/portal/css/gridview.css" />

<script type="text/javascript" src="/isml/source/portal/assets/91c257bb/jquery.js"></script>

<script type="text/javascript" src="/isml/source/portal/assets/91c257bb/jquery.ba-bbq.js"></script>

<title>ISML</title>

<link href="/isml/source/portal/css/master.css"rel="stylesheet" type="text/css" />

<link rel="icon" type="/isml/source/portal/image/png" href="images/favi-icon.ico"/>

<script type="text/javascript" src="/isml/source/portal/js/jquery.min.js"></script>

<script type="text/javascript" src="/isml/source/portal/js/slides.min.jquery.js"></script>

<script type="text/javascript" src="/isml/source/portal/js/jquery_carousel_lite.js"></script>

<script type="text/javascript" src="/isml/source/portal/js/jquery.jqzoom-core.js"></script>

<script type="text/javascript" src="/isml/source/portal/js/placeholder.js"></script>

<script type="text/javascript" src="/isml/source/portal/js/script.js"></script>


</head>



Can anyone point me where i am wrong?..

If you need only custom page size for cgridview you can implement this solution.

  1. Add a textbox or dropdownlist in the page to change the page size;

  2. Add beforeAjaxUpdate event to CGridView;

  3. If you have a dropdownlist and you want to refresh your grid after value change, use jquery to observe change event;

  4. In method that return DataProvider (in your case Notification List) capture request value for the page size;

As below:

in grid.php




<?php

Yii::app()->clientScript->registerScript('js_beforeAjaxUpdate', "

    function gridServiziBeforeAjaxUpdate(id, options)

    {

        options.data = new Object();

        options.data.pageSizeInputValue = $('select#pageSizeInputValue').val();

    }

    $('select#pageSizeInputValue').on('change', function(ev) {

        $.fn.yiiGridView.update('servizi-grid');

    });

");

?>


<div style="float:right">

View # for page: <?php echo CHtml::dropDownList('pageSizeInputValue', null, array(30=>30,35=>35,40=>40,45=>45,50=>50,55=>55,60=>60)); ?>

</div>


<?php $this->widget('GridViewServizi', array(

    'id'=>'servizi-grid',

    'dataProvider'=>$model->search(),

    'filter'=>$model,

	'modelBaseSearch' => $model,

	'beforeAjaxUpdate' => 'gridServiziBeforeAjaxUpdate',

      ...

      ...

?>




in model’s method that return dataprovider




        $pageSizeInputValue = Yii::app()->request->getParam('pageSizeInputValue', 30);

        ...

        ...

    		return new CActiveDataProvider($this, array(

             'pagination'=>array('pageSize'=>$pageSizeInputValue),

			'criteria'=>$criteria,

		));



Yup… I used a custom drop down menu and passed the corresponding values and it worked perfect…

Thanks a lot for replying…:)

Hi sir , I have been facing the same problem for few days and I have also tried your suggestion too but still getting the same error


$.params.querystring

is not a function…