CLinkPager withing CListView

I’m using CListView to display items on a page. In my model, I use search() method to return CActiveDataProvider and to use it in my CListView widget.

My problem is with pagination. I want to use paggination with customized labels for next and previous buttons/links. My code is the following:


 return new CActiveDataProvider($this, array(

                    'criteria'=>$criteria,

                    'pagination'=>array(

                        'pageSize'=>2,

                        'class'=>"CLinkPager"

            ),

            ));

When I start my application, I get the following error:

In this post http://www.yiiframework.com/forum/index.php/topic/26563-how-to-implement-custom-pagination/ I can see that @ enfield advised to use or extend the CLinkPager, and before doing that, is there any other way?

When I delete the line ‘class’=>“CLinkPager” everything works fine.

I have never used custom pagers and stuff but the yii documentation should provide you with hints on what to do. The pagination attribute in CActiveDataProvider uses a CPagination instance, not a CLinkPager instance. That’s why you are getting undefined errors because Yii is looking for stuff related to that CPagination class.

A CLinkPager class can be passed to the widgets themselves like CGridView and CListView. Try this with your clistview:




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

    'dataProvider' => $dataProvider,

    'itemView' => '_view',

    'pager' => array(

		'class' => 'MyCustomCLinkPager',

	),

));



Don’t define your pager in the dataprovider, but define it in CListView.

Unfortunately, your solution/advice is not working. If I set pageSize attribute to 2, more than 2 items are displayed on the page (user @NewToYii had the same issue with CGridView http://www.yiiframework.com/forum/index.php/topic/11498-cgridview-and-pagesize/)

Ok, finally I found the solution. If anyone needs to use CListView with customized labels in the CLinkPager, and the different number of items per page, you should do it this way:

[size="4"]Desired model class[/size]

In your model class method which returns CActiveDataProvider specify the pageSize attribute (do not specify pageSize attribute in the CLinkPager in the view).


return new CActiveDataProvider($this, array(

                    'criteria'=>$criteria,

                    'pagination'=>array(

                        'pageSize'=>  7//Or you can evaluate expression

            ),

            ));

[size="4"]View file[/size]

This way, you just specified the size of the page, but you didn’t specify pager class. You should do that in the view file:


$widget=$this->widget('zii.widgets.CListView', array(

    'dataProvider' => $dataProvider,

	'itemView' => '_viewFIle',

	'pagerCssClass' => 'result-list',

	'summaryText' => 'Number of results: {count}',

        'enableSorting'=>true,

        'enablePagination'=>true,

        'sorterHeader'=>"Sort by: ",

        'sortableAttributes'=>array(

            'name',

            'size',

        ),

        'pager' => array(

                'class' => 'CLinkPager',

            'nextPageLabel'=>"Go to next",

            'prevPageLabel'=>"Go to previous"

        ),

        "emptyText"=>"Ups no results <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />"

      

));