Yii Framework Forum: DropDown for pageSize in CGridView - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

DropDown for pageSize in CGridView A convenient drop down to select page size and save in User state. Rate Topic: ***** 13 Votes

#21 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 09 November 2010 - 06:50 PM

View PostAntonio Ramirez, on 09 November 2010 - 06:35 PM, said:

This is a really great post. There should be a place in the wiki where, separated by objects -or within the class reference information, related wiki articles should appear for the people to learn. This is a great tip Mike, and I will surely use it in my new project. This is actually a great tip for those who wish to create more complex filters to the data displayed.

I agree! :)

If you want more filtering, consider checking out the datafilter extension:
http://www.yiiframew...sion/datafilter
"Less noise - more signal"
0

#22 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 10 November 2010 - 03:46 AM

View PostAntonio Ramirez, on 09 November 2010 - 06:35 PM, said:

This is a really great post. There should be a place in the wiki where, separated by objects -or within the class reference information, related wiki articles should appear for the people to learn. This is a great tip Mike, and I will surely use it in my new project. This is actually a great tip for those who wish to create more complex filters to the data displayed.

If you don't mind, I will like to post an article in my blog with this know-how.


Thanks. :)

The basic idea was to show, how easy you can enhance Yii's components if you're not afraid of javascript. So sure, feel free to share wherever you like.
0

#23 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,448
  • Joined: 04-October 10

Posted 10 November 2010 - 03:41 PM

Pretty good man, not bad at all.. and also the link of jacmoe...

Thank you both
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#24 User is offline   nguyendh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 21-March 11

Posted 05 April 2011 - 02:14 PM

I follow your example and set Agent and Brokerage. I add 1/2/3 items per page for testing purposes. Only Agent view is working. Brokerage view always resets the item-per-view back to 1 in the dropdownlist even though the number of items are displayed correctly, and i have no clue why dropdownlist resets to 1 every time I select a new pagesize.
Javascript issue maybe ?

p/s: after looking closely, the setup allows different view share the same pagesize.
p/s: the dropdownlist in the non-working-view does not have [selected="selected"], why is that ?

Thanks
I am new to Yii and this is my blog http://blog.michaelduy.com (deprecated hatadu.wordpress.com) about Yii, Git and PHP.
0

#25 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 06 April 2011 - 01:39 AM

Without showing the relevant code parts we can't tell much. You probably don't provide the current $pageSize to the dropdownlist in your view file. So maybe check the code for typos.
0

#26 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,060
  • Joined: 16-February 11
  • Location:Japan

Posted 30 April 2011 - 12:31 AM

Just a trivial addition to the great article. :D

The pageSize dropDownList can also be implemented as a part of the summary text, not as the button column header.

<?php $pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']); ?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'summaryText'=>'Displaying {start}-{end} of {count} result(s). ' .
        CHtml::dropDownList(
            'pageSize',
            $pageSize,
            array(5=>5,20=>20,50=>50,100=>100),
            array('class'=>'change-pageSize')) .
        ' rows per page',
    'columns'=>array(
        'id',
        'username',
        'password',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>
<?php Yii::app()->clientScript->registerScript('initPageSize',<<<EOD
    $('.change-pageSize').live('change', function() {
        $.fn.yiiGridView.update('user-grid',{ data:{ pageSize: $(this).val() }})
    });
EOD
,CClientScript::POS_READY); ?>

2

#27 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 592
  • Joined: 27-December 10
  • Location:china

Posted 05 July 2011 - 01:00 PM

View Postsoftark, on 30 April 2011 - 12:31 AM, said:

Just a trivial addition to the great article. :D

The pageSize dropDownList can also be implemented as a part of the summary text, not as the button column header.
:D

good idea , i like your thought ; cheers
0

#28 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 11 August 2011 - 04:29 PM

Thanks for the code guys. I have it working on one grid just fine but when I add it to a different grid the functionality does not work on that second grid. Is that to be expected or can one use this as many times at wanted in a given app?
0

#29 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,342
  • Joined: 12-October 09
  • Location:Croatia

Posted 12 August 2011 - 01:01 AM

It should work for any grid and you can use it for all the grids in the app...
Find more about me.... btw. Do you know your WAN IP?
0

#30 User is offline   Say_Ten 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 17-September 10

Posted 12 August 2011 - 05:04 AM

I came across this recently, my solutions was to extend the CGridView class such:

<?php
/*
 * Project : 
 * Date    : Jul 1, 2011 10:20:59 AM
 * Author  : matt.cheale
 * File    : MGridView.php
 */

Yii::import( 'zii.widgets.grid.CGridView' );

/**
 * Extends CGridView making pagers at the top and bottom default and adding in a pageSize
 * form element.
 *
 * @author matt.cheale
 * @since Jul 1, 2011
 */
class MGridView extends CGridView
{
	/**
	 * @var string overides the parent default.
	 */
	public $template = "{limit}\n{pager}\n{items}\n{pager}\n{limit}";
	
	/**
	 * @var array the option values for use in the drop down
	 */
	public $pageSizeOptions = array( '5' => '5', '10' => '10', '20' => '20', '50' => '50' );
	
	/**
	 * @var CModel the model that is used to power the search.
	 */
	public $model;
	
	/**
	 * Renders the Show X amount of items drop down.
	 */
	public function renderLimit()
	{
		if ( ! $this->model instanceof CModel )
		{
			return;
		}
		
		$refl = new ReflectionClass( $this->model );
		if ( ! $refl->hasProperty( 'pageSize' ) )
		{
			return;
		}
		
		echo CHtml::openTag( 'div', array( 'class' => 'grid-limit' ) );
		echo CHtml::label( 'Show:', null );
		echo CHtml::activeDropDownList( $this->model, 'pageSize',
			array( '5' => '5', '10' => '10', '20' => '20', '50' => '50' ), array( 'class' => 'grid-limit-field' ) );
		echo CHtml::closeTag( 'div' );
	}
	
	/**
	 * Overides parent to add in jQuery for the changing of the drop down lists.
	 */
	public function registerClientScript()
	{
		parent::registerClientScript();
		
		$id = $this->getId();
		Yii::app()->getClientScript()->registerScript( __CLASS__ . '#' . $id . '#limit-field', <<<___JS___
$('#{$this->getId()} .grid-limit-field').live( 'change', function() { $.fn.yiiGridView.update('{$id}', { data: $(this).serialize() }); return false; } );
___JS___
			, CClientScript::POS_READY );
	}
}


I'm sure it could be made more generic further though. Just thought I'd share ;)
0

#31 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 15 August 2011 - 03:41 AM

View Postmdomba, on 12 August 2011 - 01:01 AM, said:

It should work for any grid and you can use it for all the grids in the app...



After seeing your answer I revisited it today and got them working. Thanks

Since this is tied to the user state, how can a default be set for a guest user? Right now looks like the default is 10 if you are not logged in and I need that increased.
0

#32 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,342
  • Joined: 12-October 09
  • Location:Croatia

Posted 15 August 2011 - 07:06 AM

In the $model->search() you are reading the pageSize, right?

So there you can check if it's not set and if the user isGuest... and set the value you need..
Find more about me.... btw. Do you know your WAN IP?
0

#33 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 15 August 2011 - 12:19 PM

Ok I just ended up taking care of it in my config file like Mike suggested.
0

#34 User is offline   JamesBarnsley 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 420
  • Joined: 18-November 10
  • Location:UK

Posted 26 November 2012 - 01:36 PM

Hi,

I have tried this, but I am getting the following JS Error ...

TypeError: settings is undefined
[Break On This Error]

$grid.addClass(settings.loadingClass);

Any ideas, its for something to do with the Yii GridView JQuery update I think.

James.
0

#35 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 12 January 2013 - 07:00 PM

Great tip Mike ! Thank you!

I had a project requirement to offer this feature in many views and the client wanted the user model to have a default that they could maintain and each view would start with the user default as the set of data.

I had to tweak things a bit to get the user default set as they entered the view and then allow the view to control the pagesize
as the user changes it.

This was a huge help !

Thanks again!
0

#36 User is offline   datdit 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-January 13

Posted 12 January 2013 - 11:55 PM

i use extention it's work.

http://www.yiiframew...nsion/pagesize/
0

#37 User is offline   thiagovidal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 147
  • Joined: 17-February 10
  • Location:Brazil, São Paulo

Posted 01 February 2013 - 05:49 AM

Hey Mike and others. Have anyone had problems with losing filters by #search-form.serialize after change pager size?
0

#38 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 01 February 2013 - 02:20 PM

Dear Friends

That was really great.

I have something to share.

Here I have not used session.

I have a model:Edible(id,name).

I have created a virtual property pageSize in the model and made it safe on search.

class Edible extends CActiveRecord
{   
	public $pageSize=10;
	
	public function rules()
	{
	
		return array(
			array('name', 'required'),
			array('name', 'length', 'max'=>64),
			array('id, name,pageSize', 'safe', 'on'=>'search'),
		);
	}

	
	public function search()
	{
		$criteria=new CDbCriteria;

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

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array("pageSize"=>$this->pageSize),//Declaring page size.
		));
	}
}




<?php echo CHtml::activeDropDownList($model,'pageSize',array(5=>5,10=>10,20=>20,50=>50,100=>100));?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'edible-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
//calling beforeAjaxUpdate method.
	'beforeAjaxUpdate'=>'js:function(id,options)  
         {		
		var url=options.url;
		url=url+"&Edible[pageSize]="+$("#Edible_pageSize").val();
		options.url=url;		
		return true;
	 }',
	'columns'=>array(
		'id',
		'name',
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>
<?php

Yii::app()->clientScript->registerScript('test','

$("#Edible_pageSize").on("change",function(){
	$("#edible-grid").yiiGridView("update",{data:{"Edible[pageSize]":$(this).val()}});
	});
');



I am making a dropDownList at the top of the grid.

name attribute of that input="Edible[pageSize]".
id attribute of that input="Edible_pageSize.
Both are by default made by YII.

In beforeAjaxUpdate method I am appending the value of the pageSize.So it is always available when going across pages and during filtering.

I am also registering script for ajax update of GRID when user changes the value of dropDownList.

Regards.
0

#39 User is offline   subbu vemuri 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 17-May 13

Posted 24 June 2013 - 06:13 AM

// Accessable with Yii::app()->params['paramName']
'params'=>array (

'defaultPageSize'=>20,


hello sir,where can i change the params value in yii?? i mean in which page i have to change??

thank you

subbu vemuri
0

#40 User is offline   stz184 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 02-September 12

Posted 20 August 2013 - 04:55 AM

Hello, friends :)
I read all your posts and found no suitable solution but many interesting ideas and hints how I can do it.
My requirements are as follows:
  • Drop down menu allows the user to select the page size (how many rows per page to be showed in the grid)
  • User's choice is stored in user's state variable
  • The grid should works with any model, without need of model's class modification
  • The page size menu should be optional (configurable) throug the template property.


So... I extended the CGridView and wrote this:

<?php

Yii::import('zii.widgets.grid.CGridView');

/**
 * Extends CGridView, adding drop down menu that controls the page size (number of items showed per page)
 */
class ExtendedGridView extends CGridView {

	/**
	 * @var string overides the parent default.
	 */
	public $template = "{items}\n{limit}\n{pager}";

	/**
	 * @var array the option values for use in the drop down
	 */
	public $pageSizeOptions = array('5' => '5', '10' => '10', '20' => '20', '50' => '50');

	/**
	 * @var integer the default page size (items per page shown)
	 */
	protected $defaultPageSize;

	public function init() {
		//Yii::log(CVarDumper::dumpAsString($this->customHtml), 'debug');
		if(isset($_GET['pageSize']) && is_numeric($_GET['pageSize'])) {
			Yii::app()->user->setState('pageSize', intval($_GET['pageSize']));
		}
		
		$pageSize = Yii::app()->user->getState('pageSize');
		$this->defaultPageSize = is_numeric($pageSize) ? $pageSize : $this->dataProvider->pagination->pageSize;
	
		$this->dataProvider->setPagination(array(
			'pageSize' => intval($this->defaultPageSize)
		));
		
		parent::init();
	}

	/**
	 * Renders the Show X amount of items drop down.
	 */
	public function renderLimit() {		
		echo CHtml::openTag('div', array('class' => 'grid-limit'));
		echo CHtml::label(t('extendedgridview.form.label.perpage_dropdown').':', null);
		echo CHtml::dropDownList('pageSize', $this->defaultPageSize, $this->pageSizeOptions, array(
			'class'=>'grid-limit-field',
			'id'=>__CLASS__.$this->getId()
		));
		echo CHtml::closeTag('div');
	}

	/**
	 * Overides parent to add in jQuery for the changing of the drop down lists.
	 */
	public function registerClientScript() {
		parent::registerClientScript();

		$id = $this->getId();
		Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $id . '#limit-field', <<<___JS___
		$('#{$this->getId()} .grid-limit-field').live( 'change', function() { 
			$.fn.yiiGridView.update('{$id}', { data: $(this).serialize() }); 
			return false; 
		});
___JS___
		, CClientScript::POS_READY);
	}

}


I hope that somebody will benefit of my code.
Best regards,
Vladimir
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users