Cake PHP中的自定义分页 [英] Custom Pagination in Cake PHP

查看:89
本文介绍了Cake PHP中的自定义分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是cakePHP的初学者,我不会在cakePHP中创建自定义分页.

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

功能$paginator->numbers() ;它显示如下页码:

1 | 2 | 3 | 4 | ...

通过查看选项,可以使用一些选项来更改分隔符,以添加样式为css ..Etc的类.

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 ?

我在app/View/Helper/中创建了自定义分页器助手,

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

并且我已经将我的CustomPaginatorHelper添加到我的Controller的$ helpers中,如下所示:

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

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

但我收到此错误:

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

看来他不知道PaginatorHelper !!! 我应该在哪里添加自定义Paginator?

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

注意::您的函数number()将仅显示分页格式:1-20 21-40 ... etc,但没有指向我认为的页面的链接:)

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 :)

我添加了App::set('PaginatorHelper','/View/Helper/');,并且我不再收到此错误. 现在,我尝试像这样调用自定义分页器的numbers()方法:

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(); 

但我收到此错误:

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

此错误的根源是什么?我试图将我的customPaginatorHelper添加到Controller的$ helpers变量中,但是我仍然遇到相同的错误;有任何想法吗 ?

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 ?

预先感谢

推荐答案

在这里要知道的关键是,有一个分页器组件(在控制器中使用)和一个分页器助手(在视图中使用).您正在使用的是PaginatorHelper类,该类处理与分页相关联的元素的呈现.

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.

不幸的是,无法使用PaginatorHelper来完成您想实现的目标.如果要执行此操作,最好的方法是扩展PaginatorHelper类并重写numbers()方法以返回所需的内容.

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.

我已经看过那个特定的方法了,可悲的是它并不好-超过100行!但是,我创建了一个类,该类继承了PaginatorHelper并重写了该方法.由于原始方法太长,因此需要大量复制和粘贴,因此,我没有将其直接放在此答案中.

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.

您可以在此处查看: https://gist.github.com/2037902

您还需要在控制器中的助手列表中添加CustomPaginator.

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

这篇关于Cake PHP中的自定义分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆