pagination

Hello guys.

I’m using CGridView with pagination and it’s a great widget. But pages are displayed in this way


 1 2 3 4 5 6 7 8 9 10 

It’s Ok when there are not too many pages, but what if there are 1000 pages or more? User will get tired of clicking page numbers to list all of them.

Is it posible to display page numbers like this


1 2 3 4 5 6 7 8 9 ... 20 21

?

Thank you.

I think that is not possible to configure the pager like that.

You can write your own pager and then configure CGridView for use your one. There is the property ‘pager’=>‘class’ for this.

Ok, I decided to follow zaccaria’s advice and made a little class that extends CLinkPager. I added to existing pager a dropdown list with page numbers. When you choose a number the corresponding page opens.

In my class (I called it ExtPager) I override just one method (createPageButtons()) from CLinkPager. Here is what I added to it


//select page

$pages=array();

$pages[] = '';

for($i=0;$i<$pageCount;$i++)

{

   $pages[] = $i+1;

}

                

$id = $this->controller->getUniqueId()."_ddpl";

$buttons[] = CHtml::dropDownList('pages','',$pages,array('class'=>'extpager','id'=>$id,'onchange'=>'valchange()'));

Yii::app()->clientScript->registerScript($id."_s001","

        function valchange()

        {

           page = $('#$id').val();

           if(page)

           {

              url = '".$this->createPageUrl(1)."';

              p = url.lastIndexOf('=');

              new_url = url.substr(0,p+1);

              window.location = new_url+page;

            }

            return false;

         }

         ",CClientScript::POS_HEAD);

$url = Yii::app()->request->baseUrl.'/css/extpager.css';

Yii::app()->getClientScript()->registerCssFile($url);

It’s rather rough implementation (I’m not a great programmer you know :) ) but it works.

Another disadvantage of my code is that it does not use ajax like standard pager does(I just could not figure out how to make it use ajax).

To use my ExtPager do this:

  1. save ExtPager.php in /protected/components;

  2. save extpager.css in /css folder;

  3. in your widget (e.g. CGridView) configuration add


'pager'=>array('class'=>'ExtPager'),

And that’s all.

I know that there are many good programmers on our forum so please help to improve my class.

hello i’m trying to use your pagination script, it seems good to me, but i’ve got an error like this…

Error 404

The system is unable to find the requested action "2".

it means cannot render page 2, even the first page when I back from page 2 to 1. do you have any clue… ?