dclowd9901
dclowd9901

Reputation: 6836

How do I create multiple pagination instances for a single model/controller in CakePHP

I have a view with two different actions of the same model (An item list sorted by the most recent, and the same list sorted by the most popular). One list I want to paginate with 8 items at a time and one with 12 items at a time. I'm using AJAX to page in the items. How do I go about setting up the controller to handle this task? The problem seems to be that I can't create two different pagination instances, so I can only paginate in the same number of items regardless of the action.

Thanks in advance for your help.

What I've tried so far

Basically trying to set limits specific to each action, rather than the initial pagination helper:

var $paginate = array('order'=>array('Category.name'), 'limit'=>'8');

That's what the helper looks like, and it feeds in whatever the limit is. Even if I remove that limit and try to apply it like so:

var $paginate = array('order'=>array('Item.name'));

function discover_list(){    
    $this->set('d_ajax_items', $this->paginate('Item', array('Item.discover_order' => null, 'Item.moderated' => 1), null, 'Item.id DESC', null, 8));
}

function trend_paging(){
    $this->set('trend_items', $this->paginate('Item', array('Item.moderated' => 1),NULL,  'rank DESC', NULL, 12));  
}

The limit on the end of the action is ignored, and it feeds in some default 20 items at a time

Upvotes: 0

Views: 1693

Answers (3)

Leo
Leo

Reputation: 6571

I've done this before and I wished I hadn't. Keeping the two in synch is confusing for the end user and a nightmare for the developer.

What have you tried already? I'm sure you can have two instances, but you'll need to pass the parameters directly into the function paginate() rather than setting them on the class variable paginate.

Edit

To avoid confusion I almost always define the pagination options in the call itself.

In your second example, you pass six parameters to paginate() but it only takes three:

function paginate($object = null, $scope = array(), $whitelist = array())

If you omit $object and pass an array as the first arg, the last two args move to the left.

Have you tried entering the order and limit values as an array in the third position ($whitelist)?

http://api13.cakephp.org/class/controller#method-Controllerpaginate

http://api13.cakephp.org/view_source/controller/#line-1056

Upvotes: 1

Mauro Zadunaisky
Mauro Zadunaisky

Reputation: 828

I misunderstood your original question. If you want to set different limits to each action, you can do this

function discover_list() {    
    $this->paginate['limit'] = 8;
    $this->paginate['conditions'] = array('Item.discover_order' => null, 'Item.moderated' => 1);
    $this->set('d_ajax_items', $this->paginate('Item'));
}

Edit

A little nice way

$this->paginate = array(
    'limit' => 8,
    'conditions' => array('Item.discover_order' => null, 'Item.moderated' => 1),
);
$this->set('d_ajax_items', $this->paginate('Item'));

Upvotes: 3

Mauro Zadunaisky
Mauro Zadunaisky

Reputation: 828

You can start your page with 2 empty divs and call via ajax 2 different urls mapped to controller/action. Then you only have to worry about ajax pagination, but you won’t have any cakephp issue because each pagination works into his own action.

This is not a really good approach if you want unbostrusive javascript, but if your entire app require javascript, it will work.

Upvotes: 0

Related Questions