Yii 1.1: pagesize

Widget for selecting Grid View page size
21 followers

Simple widget for select Grid View page size

Screen shot

page size

Requirements

Yii 1.1 or above

Usage

// Extract and put PageSize folder into extensions directory
// Place the widget just before the GridView
<div align="right" class="row">
<?php 
$this->widget('application.extensions.PageSize.PageSize', array(
        'mGridId' => '<grid-id>', //Gridview id
    'mPageSize' => @$_GET['pageSize'],
        'mDefPageSize' => Yii::app()->params['defaultPageSize'],
        'mPageSizeOptions'=>Yii::app()->params['pageSizeOptions'],// Optional, you can use with the widget default
)); 
?>
</div>
 
// Add below to your model search()
'pagination' => array(  'pageSize'=>Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
)
 
// Add below to your config/main.php params
'defaultPageSize' = 10,
'pageSizeOptions'=>array(10=>10,20=>20,50=>50,100=>100),
 
 
// Thats it.

Note: if you having issue with ajax submission fix this issue of framework

Total 8 comments

#9643 report it
maher at 2012/08/31 10:17am
no links in the page numbers

the page seems without any 'href' an empty link when i click in page2 for example nothing happen i used firebug and i found that there is a request with this parameter

ajax    mail-grid

what do you think is the problem?

#8642 report it
banamlehsb at 2012/06/15 04:02am
pageSize in cbutton column

how can i make it as header of cbutton column in cgridview?

#6975 report it
yiqing95 at 2012/02/16 10:45am
you are welcome , glad to help

@aruna470 about the dropdownlistHtmlOptions , i am not sure what's the best way to do that ;
you see in my code :

if(isset($this->dropDownListHtmlOptions['name'])){
                unset($this->dropDownListHtmlOptions['name']);
            }

i originally thought the generated DropDownList can used in a form block ,and enven if the js is disabled the action of controller can handle it !

$class4ddl = __CLASS__.'-'.$this->id . time();

this is for unique porpus . multi wedget instances are possible !

one can use dropDownListHtmlOptions to register his/her onchange js eventHandler ,we shouldn't swollow it .(so i use a unique class for change event !)

so feel free to modify it . :)

#6960 report it
Aruna Attanayake at 2012/02/16 02:41am
modified version

@yiqing95 Thank you for your modifications. I will update them.

#6916 report it
yiqing95 at 2012/02/13 10:59am
here i share my modified version , can use for both CGirdView and CListView;

@aruna470 i never mind you use my new version as yours( as 2.0 ) :) , because this version is based on your works , feel free to use it in anywhere:

<?php
/**
 * Date: 12-2-13
 * Time: pm 8:22
 *  @author Aruna Attanayake <aruna470@gmail.com> && yiqing95 <yiqing_95@qq.com>
 */
class EPageSize extends CWidget
{
    /**
     * @var array
     * the optional pageSize you can select
     */
    public $pageSizeOptions = array(10=>10, 25=>25, 50=>50, 75=>75, 100=>100);
 
    /**
     * @var int
     * current pageSize will be used for
     * GridView or ListView
     */
    public $pageSize = 10;
 
    /**
     * @var string
     * ---------------------
     * the gridViewId
     * ---------------------
     */
    public $gridViewId = '';
 
    /**
     * @var string
     * --------------------
     * the listViewId
     * --------------------
     */
    public $listViewId = '';
 
    /**
     * @var int
     * ----------------------
     * default pageSize for gridView or listView
     * ----------------------
     */
    public $defaultPageSize = 10;
 
    public $beforeLabel = 'select a pageSize:';
 
    /**
     * @var array
     * -------------------------------------
     * the htmlOptions passed to the underlying dropDownList
     *  note:  the name options will be ignored , will always use
     *  the pageSize .
     * -------------------------------------
     */
    public $dropDownListHtmlOptions = array();
 
    /**
     * @var string
     * -----------------------
     * the list type , the CGridView and the CListView
     * both inherit from CBaseListView , so they are all
     * listView ;  this param used to determine the sub type of CBaseListView
     *
     * -----------------------
     */
    protected $listViewType = 'Grid';
    /**
     * @var string
     * ------------------------
     * the CGridView or CListView id
     * ------------------------
     */
    protected $updateId ;
    /**
     * @throws CException
     */
    public function init(){
        if(! empty($this->gridViewId)){
            $this->listViewType = 'Grid';
            $this->updateId = $this->gridViewId;
        }elseif(! empty($this->listViewId)){
            $this->listViewType = 'List';
            $this->updateId = $this->listViewId ;
        }else{
            //if(empty($this->gridViewId) || empty($this->listViewId))
            throw new CException('you must specify the gridViewId or listViewId for using this widget!');
        }
 
        parent::init();
    }
 
    public function run()
    {
        //>  store the current pageSize to user 's session
        Yii::app()->user->setState('pageSize', $this->pageSize);
        //>  current pageSize to initialize the dropDownList
        $this->pageSize = (null == $this->pageSize) ? $this->defaultPageSize : $this->pageSize;
        //> the class property for the DropDownList
        $class4ddl = __CLASS__.'-'.$this->id . time();
 
        // handle the  ddlHtmlOptions , we add a unique css class to the dropDownList, and unset the name property.
        if(is_array($this->dropDownListHtmlOptions)){
            if(isset($this->dropDownListHtmlOptions['name'])){
                unset($this->dropDownListHtmlOptions['name']);
            }
           if(isset($this->dropDownListHtmlOptions['class'])){
               $this->dropDownListHtmlOptions['class'] .= (' '.$class4ddl);
           }else{
              $this->dropDownListHtmlOptions['class'] = ' '.$class4ddl;
           }
        }else{
            $this->dropDownListHtmlOptions = array('class' => $class4ddl);
        }
 
        echo $this->beforeLabel ,
         CHtml::dropDownList('pageSize', $this->pageSize, $this->pageSizeOptions,$this->dropDownListHtmlOptions);
 
        $jsPluginName = "$.fn.yii{$this->listViewType}View";
 
        $jsCode = <<<ON_CHANGE
         $(".{$class4ddl}").change(function(){
             var url = {$jsPluginName}.getUrl('{$this->updateId}'),
                 pageSize = $(this).val();
             //handle the url String ,  pathInfo and queryString
             url = url.replace(/pageSize\/\d+/, "pageSize/"+pageSize);
             url = url.replace(/pageSize=\d+/, "pageSize="+pageSize);
             /*  you can refer here for Regexp study:https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions 
 */
 
             {$jsPluginName}.update('{$this->updateId}',{url:url,data:{pageSize:$(this).val() }} );
         });
ON_CHANGE;
 
       Yii::app()->getClientScript()->registerScript($class4ddl,$jsCode,CClientScript::POS_READY);
 
    }
}

the usage is same to pageSize extension , in you index.php you may need a little modify :

<?php
    $this->widget('ext.PageSize.EPageSize', array(
    'listViewId' => 'user_list',
    'pageSize' => Yii::app()->request->getParam('pageSize',null),
    'defaultPageSize' =>  10 ,   // may use this :  Yii::app()->params['defaultPageSize'],
    'pageSizeOptions'=> array(5=>5, 10=>10, 25=>25, 50=>50, 75=>75, 100=>100), // you can config it in main.php under the config dir . Yii::app()->params['pageSizeOptions'],// Optional, you can use with the widget default
    ));
    ?>
 
 
<?php
$dataProvider = $model->search();
$pageSize = Yii::app()->user->getState('pageSize',10/*Yii::app()->params['defaultPageSize']*/);
$dataProvider->getPagination()->setPageSize($pageSize);
 
$this->widget('ext.bootstrap.widgets.BootListView',array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
        'id' => 'user_list'
)); ?>

you see every thing is ok . i just use the bootstrap extension and it works ,i didn't test the CListView ,but it should works too !

additionally you can give a htmlOptions for the CDropDownList , config the beforeLabel in your own language or just set it to empty string '' ;

#6915 report it
yiqing95 at 2012/02/13 10:02am
another usage example !

it 's possible to use it in gii/giix to generate your admin view: and no need to modify your model class :

$dataProvider = $model->search();
  $pageSize = Yii::app()->user->getState('pageSize',10/*Yii::app()->params['defaultPageSize']*/);
  $dataProvider->getPagination()->setPageSize($pageSize);

i v modified the original ext , want to support both CGridView and CListView ,but encounter some problem , the js function $.fn.yiiGridView.update/$.fn.yiiListView internal use this code to generate url: options.url = $.param.querystring(options.url, options.data); in pathInfo mode ,after you selected the pageSize and click some pageButton some thing strange happens , see the address of the pageLink such as : http://localhost/myProjName/user/admin/pageSize/5/ajax/user-grid/User_page/3?ajax=user-grid&pageSize=75

you see the latter pageSize will do not work

#6725 report it
jmariani at 2012/02/01 09:01am
Thank you!

Good work!

#6038 report it
GoorooTech at 2011/12/07 08:35am
Works beautifully in 1.1.8, including ajax

Works beautifully with no problems, including ajax submission, in 1.1.8 (no need to do the 'fix' in 1.1.8)

Just make sure, 'files-grid' matches your cgridview's 'id'

'mGridId' => 'files-grid'

Thx for the plugin!

Leave a comment

Please to leave your comment.

Create extension