views:

38

answers:

2

I am trying to use Ajax pagination in CakePHP and have read you can pass the variables along in the URL however I cannot seem to set the route. When I click on next I get an error about the category not existing (because of a custom validation method I created) because the route isn't being followed.

I would like the $paginator->next() and $paginator->prev() to follow this scheme of routes http://mysite.com/computers/page:2 or http://mysite.com/computers/mac/page:2. However no matter what I set the pagination URL to it always ends up becoming http://mysite.com/Products/index/page:2. I have tried setting the URL in the $paginator->options() and in the $paginator->next() and $paginator->prev() however it is not working. Any ideas on how to make cakephp paginator follow my custom routes?

Edit:

Here is my $paginator code that is in the view.

$paginator->options(array(

    'update' => '#products',
    'url' => array('controller'=>'Products','action'=>'index','pass'=>$this->params['pass']),
    'evalScripts' => true,
    'before' => $this->Js->get('#busy-indicator')->effect('fadeIn', array('buffer' => false)),
    'complete' => $this->Js->get('#busy-indicator')->effect('fadeOut', array('buffer' => false)),
));
     echo $paginator->prev("Prev",$this->passedArgs);
     echo $paginator->numbers(array('separator'=>' '));
     echo $paginator->next("Next",$this->passedArgs);
A: 

If you set paginator options it should do the trick. Try this:

$options = array('update'=>'results','url'=> array('controller' => 'companies', 'action' => 'dashboard', 'admin' => true));
$paginator->options($options);

That works perfectly for me everywhere.

FYI, I use the 'update' param with jQuery to update various areas.

zmonteca
@zmonteca wont that still make the url http://mysite.com/Products/index?
jostster
@jostster You can make the route whatever you want. By default, passing no options will make the route the default page. I'm guessing in your case that will result in /Products/index/page:2.
zmonteca
Try this: $options = array('url'=> array('controller' => 'computers', 'action' => 'index', 'type' => 'mac')); $paginator->options($options);
zmonteca
A: 

I figured it out. I see someone has favorited this so I will show my resolution in hopes that it saves them a few days of frustration trying to find a fix.

In my index.ctp I set the $paginator->options path to my url I needed.

$paginator->options(array(

    'update' => '#products',
    'url' => array('path'=>$this->params['path']),
    'evalScripts' => true,
    'before' => $this->Js->get('#busy-indicator')->effect('fadeIn', array('buffer' => false)),
    'complete' => $this->Js->get('#busy-indicator')->effect('fadeOut', array('buffer' => false)),
));

Edit: I just found out this only works if you don't add anything else to the url field... :-(

Edit 2: I ended up having to do this in my view where the $pagination is created.

$paginator->options(array(

    'update' => '#products',
    'url' => '/'.$this->here,
    //'evalScripts' => true,
    'before' => $this->Js->get('.clothing')->effect('fadeOut', array('speed' => 'slow')),
    'complete' => $this->Js->get('.clothing')->effect('fadeIn', array('speed' => 'slow')),
));
     echo str_replace('/Products/index','',$paginator->prev("Prev "));
     echo str_replace('/Products/index','',$paginator->numbers(array('separator'=>' - ')));
     echo str_replace('/Products/index','',$paginator->next(" Next"));
jostster