Paging Size Of Cgridview

Searching in the forum i found a easy way to select paging size of Cgridview

But actually need do it with a CArrayDataProvider not with CRUD auto generated code, trying this obtain the next error:


Trying to get property of non-object.

In the post explain a similar error, but that isn’t my case because i’m not calling any item from the dataprovider to build the button Element.

Here is my code

In the view




	CVarDumper::dump(Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),10,true);exit;

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

	'id'=>'extension-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array('name'=>'Name', 'header'=>'Name'),

		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('user-grid',{ data:{pageSize: $(this).val() }})",		

				)

				),			

	),

	),

));




I think the error be in the view, because in the first load the page is ok, but when the page tries to load again i get the error.

Even if i keep this code, the same error is raised


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

	'id'=>'extension-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array('name'=>'Name', 'header'=>'Name'),

		array('class'=>'CButtonColumn',

			 		

	),

	),

));

I will appreciate your help…

Hi bachem,

You have to call $.fn.yiiGridView.update with the proper id of the grid. Not ‘user-grid’ but ‘extension-grid’.

You are right, but that doesn’t solve the problem, i get the same error.

Thanks a lot softark

I see.

You might have a problem in constructing the array data provider. Don’t you try to handle the offset and the limit by yourself?

Actually not, but if i remove the CButtonColumn everything is ok.

Probably you have to set ‘viewButtonUrl’, ‘updateButtonUrl’ and ‘deleteButtonUrl’ manually when you are working with CArrayDataProvider.

Though I’m not sure because I have never used CArrayDataProvider with CButtonColumn, CButtonColumn seems to use “$data->id” to get the id, but it must be “$data[‘id’]” when you are working with CArrayDataProvider.




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

    'id'=>'extension-grid',

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        array(

            'name'=>'Name',

            'header'=>'Name'

        ),

        array(

            'class'=>'CButtonColumn',

            'viewButtonUrl'=>"CHtml::normalizeUrl(array('view', 'id'=>\$data['id'])",

            'updateButtonUrl'=>"CHtml::normalizeUrl(array('update', 'id'=>\$data['id'])",

            'deleteButtonUrl'=>"CHtml::normalizeUrl(array('delete', 'id'=>\$data['id'])",

            'header'=>CHtml::dropDownList(

                "pageSize",

                "",

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

                array(

                    "onchange"=>"$.fn.yiiGridView.update('extension-grid',{ data:{pageSize: $(this).val() }})",

                )

            ),

        ),

    )

);



I found an alternative solution and look like the attachment screenshot

at the end of the grid




<div id="pag_items">

	<?php

		echo Yii::t('app', 'Page size:'). " ";

		echo CHtml::dropDownList('pageSize', Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']), array(10=>10, 20=>20,50=>50,100=>100,));

	?>

</div>



js




$("#pageSize").click(function(){

    $.fn.yiiGridView.update('extension-grid', {

		data: $(this).serialize()

	});

})



And the rest of the code keep like the Antonio Ramirez contribution

Thanks a lot softark