Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Pagination in Cake PHP

i'm a beginner in cakePHP , and i wan't to create a custom pagination in cakePHP .

the function $paginator->numbers() ; it displays the page numbers like this :

1 | 2 | 3 | 4 | ...

by looking in the options , there is some options to change the separator, to add a class of a style css ..Etc .

what i want is to have my pagination like this :

1-20 21-40 41-60 61-80 ... >>

some one has an idea about how to code it ?

EDIT :

i've created the custom paginator helper in : app/View/Helper/ ,

and i've added my CustomPaginatorHelper to $helpers of my Controller like this :

public $helpers = array('CustomPaginator', 'Html', 'Form', 'Js');

but i got this error :

Fatal error: Class 'PaginatorHelper' not found in /Applications/MAMP/htdocs/QRCode/app/View/Helper/CustomPaginatorHelper.php on line 2

it seems that he doesn't know the PaginatorHelper !!! Where i should add my custom Paginator ??

NB : your function numbers() will display just the format of the pagination : 1-20 21-40 ...etc , but without links to the pages i think :)

EDIT 2 :

i've added App::set('PaginatorHelper','/View/Helper/'); and i don't get this error anymore. Now i try to call the numbers() method of the custom paginator like this :

$this->CustomPaginator->numbers(); 

but i get this error :

Fatal error: Call to a member function numbers() on a non-object in /Applications/MAMP/htdocs/QRCode/app/View/Codes/index.ctp on line 71

what is the source of this error ? i've tried to add my customPaginatorHelper to the $helpers variable of my Controller but i still get the same error ; any ideas ?

thanks in advance

like image 733
Houcine Avatar asked Feb 21 '23 05:02

Houcine


1 Answers

The key thing to know here is that there is a paginator component (used in the controller) and a paginator helper (used in the view). What you're using is the PaginatorHelper class, which handles the rendering of the elements associated with pagination.

Unfortunately, there's no way of doing what you want to achieve with the PaginatorHelper. The best approach if you want to do this would be to extend the PaginatorHelper class and override the numbers() method to return what you want.

I've taken a look at that particular method, and sadly it isn't nice - it's over 100 lines long! However, I've created a class that subclasses the PaginatorHelper and overrides the method. It's a lot of copy and paste as the original method is so long, and for that reason I've not put it directly in this answer.

You can view it here: https://gist.github.com/2037902

You also need to add CustomPaginator to the list of helpers in the controller.

like image 64
Jon Cairns Avatar answered Feb 28 '23 21:02

Jon Cairns