User
User

Reputation: 66081

How to control page get parameter in Yii CListView?

Using Yii 1.1.12. I have a CListView with ajax turned off:

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'ajaxUpdate' => false,
)); ?>

The url for the link to the second page looks like:

http://www.example.com/products?Product_page=2

I want the url to look like:

http://www.example.com/products?page=2

How can I achieve this?

(A bonus would be to also describe how to get a url like http://www.example.com/products/page/2)

Update

As pointed out by elbek, I see the CListView has a pager property of type CLinkPager. This in turn has a pages property of type CPagination which has a property pageVar which is described as:

name of the GET variable storing the current page index. Defaults to 'page'.

Sounds like it could be what I'm looking for, not sure how to modify this from the CListView though.

Upvotes: 1

Views: 6194

Answers (3)

bool.dev
bool.dev

Reputation: 17478

Additionally to change the pageVar you have to modify the $dataProvider. You can do this when you are defining it in the controller action:

$dataProvider=new CActiveDataProvider('Products',array(
    'pagination'=>array(
        'pageVar'=>'page'
    )
));

Alternatively you can of course modify the $dataProvider in the view itself before using it in the CListView : (not recommended-owing to separation of concerns)

$dataProvider->pagination=array('pageVar'=>'page');
$this->widget('zii.widgets.CListView', array(/*rest of your code*/));

But with this change, to make your url look like http://www.example.com/products/page/2 you'll need to slightly change the rule in urlManager from Suvera's answer:

'products/page/<page:\d+>'=>'products/index',

Note: If you don't need that type of url, you don't need the above rule, just specifying the pageVar does it.

Upvotes: 5

SuVeRa
SuVeRa

Reputation: 2904

enable the urlManager component on your config. And add the following rule at the top.

'urlManager'=>array(
    ......
    'rules'=>array(
        'products/page/<Product_page:\d+>'=>'products/index', //Add it in top
        ...........
        ...........
    ),
),

the above rule will create a url something like http://www.example.com/products/page/2

The value part in the rule products/index is products controller and index action (this is important, so point it out to your actual route).

you can even create urls whatever way you want.

ex 1:

'products/<Product_page:\d+>'=>'products/index'

will give you http://www.example.com/products/2

ex 2

'TheAvengers/vs/Loki/<Product_page:\d+>'=>'products/index'

will give you http://www.example.com/TheAvengers/vs/Loki/2

Upvotes: 3

Elbek
Elbek

Reputation: 3494

I think CListView has pager attribute(from its parent class)

You can try to set some attributes for this. I think it is CLinkPager instance.

Upvotes: 0

Related Questions