7

I am new to Yii. I want to implement custom pagination. I want to change the appearance of the pager. How do I change the labels of the pager's links?

I want the links to appear like so:

<<  <  1  2  3  4  >  >>

instead of their default appearance, which is like this:

[first] [previous]  1  2  3  4  [next] [last]

I am using CListView to display the data, which I have set up like this:

$this->widget('zii.widgets.CListView', array(
            'dataProvider' => $categoryProjects,
            'itemView' => '_itemDetailsView',           
            'ajaxUpdate'=>false,
        ));

Can anyone please tell me how do I start with it? I've seen some posts but unable to get right information.

Thanks in advance.

Jon
  • 428,835
  • 81
  • 738
  • 806
S K
  • 177
  • 1
  • 2
  • 10

2 Answers2

12

You need to set the pager property of the CListView. By default, this is a CLinkPager; you don't need to change that (this component has your needs covered), but you need to configure it:

$this->widget('zii.widgets.CListView', array(
            'dataProvider' => $categoryProjects,
            'itemView'     => '_itemDetailsView',
            'ajaxUpdate'   => false,
            'pager'        => array(
                                'class'          => 'CLinkPager',
                                'firstPageLabel' => '<<',
                                'prevPageLabel'  => '<',
                                'nextPageLabel'  => '>',
                                'lastPageLabel'  => '>>',
                              ),
        ));

Update: If you want to "bake in" the above custom configuration to all list views in your application, you have to create a new CustomListView component deriving from CListView. So you need this class:

Yii::import('zii.widgets.CListView');

class CustomListView extends CListView {
    public function init() {
        parent::init();

        $this->pager = array( 
                            'class'          => 'CLinkPager', 
                            'firstPageLabel' => '<<', 
                            'prevPageLabel'  => '<', 
                            'nextPageLabel'  => '>', 
                            'lastPageLabel'  => '>>', 
                       );
    }
}

After including this, you can simply use CustomListView as your list widget instead of zii.widgets.CListView.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks Jon for the quick reply. Is there a way to implement this in a single file and it will be applied automatically to all the files. I've used this cListView in many files. And also where can I change the CSS for page numbers? – S K Dec 08 '11 at 03:13
  • @RSK: Please see [this discussion](http://www.yiiframework.com/forum/index.php?/topic/26055-change-the-general-pager-css-for-all-application/) for the CSS. I will shortly be updating the answer for your other question. – Jon Dec 08 '11 at 03:15
  • How do I limit the page numbers to 5? The default settings showing 10 page numbers. I've tried this public $maxButtonCount=5; but this isn't working for me. Any suggestions? Also I want to change the layout a bit to like this < 1 ... 8 9 10 11 12 .... 30 > because it is showing 10 pages.. – S K Dec 08 '11 at 07:44
1

You can refer the link:

Yii2: How to setup pagination style and other labels

Here you will get most of the options to set up for the custom pagination labels

Rahul Mankar
  • 910
  • 9
  • 17