Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax Pagination in Cakephp not setting page

I have followed the CakeBook to create Ajax based pagination. The Links are all working, the routes are working because it is sending the request to the correct action on the controller. I am getting the relevant page number in my controller action and I am then sending it to the Paginate helper to fetch the data. But that only ever returns the first page of results irrespective of the page being set. So I did an override of the paginate function on the model and sure enough I am always getting the page set to 1.

Controller Action code..

public function reviews(){
   if ($this->RequestHandler->isAjax()) { 
        if(!empty($this->params['page'])) {
            $review = $this->Page->find('first',     array('conditions'=>array('friendly'=>'reviews')));
            $this->layout = 'ajax';
            $this->paginate = array(
                'page' => $this->params['page'],
                'limit' => 3,
                'order' => array(
                    'Review.rating'=>'desc')
            );
            $reviews = $this->paginate('Review');       
            $this->set(compact('review','reviews'));
        }
} 
}

So how do I get the Paginator to use my page that I am sending it?

like image 435
Dave Gill Avatar asked Nov 25 '25 18:11

Dave Gill


1 Answers

Okay have found a solution. The default for the pagination is to used named parameters. Which gives you URLs like blah/page:6. I am not sure why but if you leave the Pagination array alone and do not set the page then it seems to ignore the page number in the request. So I changed the options in the pagination to

                $this->paginate = array('Review'=>array(
                'paramType' => 'querystring',
                'page' => $this->params['page'],
                'limit' => 3,
                'order' => array(
                    'Review.rating'=>'desc')
            ));

This then did not go through the routing properly and I was getting the wrong page from the AJAX request. Which made me think it might have been a routing issue.... I did not follow this too far as I then just forced the route when creating the paging links by setting the Pagination options on the view to:-

$this->Paginator->options(array(
    'update' => '#tabReviews',
    'evalScripts' => true,
    "url"=>(array("controller"=>'home',"action"=>'reviews',"plugin"=>'tyres'))
));

This is now working so I am hoping that it helps someone else with the same problem.

like image 157
Dave Gill Avatar answered Nov 28 '25 06:11

Dave Gill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!