简体   繁体   English

蛋糕PHP中的自定义分页

[英]Custom Pagination in Cake PHP

i'm a beginner in cakePHP , and i wan't to create a custom pagination in cakePHP . 我是cakePHP的初学者,我不想在cakePHP中创建自定义分页。

the function $paginator->numbers() ; 函数$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 . 通过查看选项,有一些选项可以更改分隔符,添加一个样式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/ , 我在: app/View/Helper/创建了自定义分页app/View/Helper/

and i've added my CustomPaginatorHelper to $ helpers of my Controller like this : 我已经将我的CustomPaginatorHelper添加到我的Controller $ helpers ,如下所示:

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 !!! 看来他不知道PaginatorHelper !!! Where i should add my custom Paginator ?? 我应该在哪里添加我的自定义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 :) 注意:您的功能号码()将只显示分页的格式:1-20 21-40 ...等,但没有链接到我认为的页面:)

EDIT 2 : 编辑2:

i've added App::set('PaginatorHelper','/View/Helper/'); 我添加了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 : 现在我尝试调用自定义分页器的numbers()方法,如下所示:

$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 ; 我试图将我的customPaginatorHelper添加到我的Controller的$ helpers变量,但我仍然得到相同的错误; any ideas ? 有任何想法吗 ?

thanks in advance 提前致谢

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). 这里要知道的关键是有一个paginator组件(在控制器中使用)和一个paginator helper(在视图中使用)。 What you're using is the PaginatorHelper class, which handles the rendering of the elements associated with pagination. 您正在使用的是PaginatorHelper类,它处理与分页相关联的元素的呈现。

Unfortunately, there's no way of doing what you want to achieve with the PaginatorHelper. 不幸的是,没有办法用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. 如果要执行此操作,最好的方法是扩展PaginatorHelper类并覆盖numbers()方法以返回所需内容。

I've taken a look at that particular method, and sadly it isn't nice - it's over 100 lines long! 我已经看过那个特殊的方法,遗憾的是它并不好 - 它超过100行! However, I've created a class that subclasses the PaginatorHelper and overrides the method. 但是,我创建了一个继承PaginatorHelper并重写该方法的类。 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 你可以在这里查看: https//gist.github.com/2037902

You also need to add CustomPaginator to the list of helpers in the controller. 您还需要将CustomPaginator添加到控制器中的帮助程序列表中。

I found even an easier way of achieving the same thing. 我发现甚至更容易实现同样的事情。 You will notice PaginationHelper extends AppHelper. 您会注意到PaginationHelper扩展了AppHelper。 So, if you copy any functions from the PaginatorHelper to the AppHelper calls and call it from the PaginationHelper, it will behave the same way and without any erros. 因此,如果您将任何函数从PaginatorHelper复制到AppHelper调用并从PaginationHelper调用它,它将以相同的方式运行,没有任何错误。

Usage 用法

$numbers_config = array(
"before" => null,
"after" => null,
"separator" => "",
"tag" => "li"
);
echo $this->Paginator->customNumbers($numbers_config);

Code

// /app/View/AppHelper.php

App::uses('Helper', 'View');

class AppHelper extends Helper {

    public function customNumbers($options = array()) {
        if ($options === true) {
            $options = array(
                'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
            );
        }

        $defaults = array(
            'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
            'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
            'currentClass' => 'current', 'currentTag' => null
        );
        $options += $defaults;

        $params = (array)$this->params($options['model']) + array('page' => 1);
        unset($options['model']);

        if ($params['pageCount'] <= 1) {
            return false;
        }

        extract($options);
        unset($options['tag'], $options['before'], $options['after'], $options['model'],
            $options['modulus'], $options['separator'], $options['first'], $options['last'],
            $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
        );

        $out = '';

        if ($modulus && $params['pageCount'] > $modulus) {
            $half = intval($modulus / 2);
            $end = $params['page'] + $half;

            if ($end > $params['pageCount']) {
                $end = $params['pageCount'];
            }
            $start = $params['page'] - ($modulus - ($end - $params['page']));
            if ($start <= 1) {
                $start = 1;
                $end = $params['page'] + ($modulus - $params['page']) + 1;
            }

            if ($first && $start > 1) {
                $offset = ($start <= (int)$first) ? $start - 1 : $first;
                if ($offset < $start - 1) {
                    $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
                } else {
                    $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
                }
            }

            $out .= $before;

            for ($i = $start; $i < $params['page']; $i++) {
                $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
            }

            if ($class) {
                $currentClass .= ' ' . $class;
            }
            if ($currentTag) {
                $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
            } else {
                $out .= $this->Html->tag($tag, "<a href='#'>".$params['page']."</a>", array('class' => $currentClass));
            }
            if ($i != $params['pageCount']) {
                $out .= $separator;
            }

            $start = $params['page'] + 1;
            for ($i = $start; $i < $end; $i++) {
                $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
            }

            if ($end != $params['page']) {
                $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
            }

            $out .= $after;

            if ($last && $end < $params['pageCount']) {
                $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
                if ($offset <= $last && $params['pageCount'] - $end > $offset) {
                    $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
                } else {
                    $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
                }
            }

        } else {
            $out .= $before;

            for ($i = 1; $i <= $params['pageCount']; $i++) {
                if ($i == $params['page']) {
                    if ($class) {
                        $currentClass .= ' ' . $class;
                    }
                    if ($currentTag) {
                        $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
                    } else {
                        $out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
                    }
                } else {
                    $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
                }
                if ($i != $params['pageCount']) {
                    $out .= $separator;
                }
            }

            $out .= $after;
        }

        return $out;
    }

}

I found even simpler solution, No Custom code needed and will be in a CakePHP's way of doing it, might not work in CakePHP 2.0 below versions cause I checked in 2.5... 我发现更简单的解决方案,需要没有自定义代码,并且将采用CakePHP的方式,可能无法在CakePHP 2.0下面的版本中工作,因为我检查了2.5 ...

For Paginators number() method you can always override its default array() which is like: 对于Paginators number()方法,您始终可以覆盖其default array() ,如:

$defaults = array(
            'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
            'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
            'currentClass' => 'current', 'currentTag' => null
);

now we can override these defaults anytime we want just use these attributes in your options array() for example in your case: 现在我们可以随时覆盖这些默认值,只需在您的options array()使用这些属性,例如在您的情况下:

    $this->Paginator->numbers(
                              array(
                                'separator' => ' - ',
                                'after' => '',
                                'before' => ''
    ));

You can show answer here 你可以在这里显示答案

Custom-Query-Pagination 定制查询,分页

You can also show another result here. 您还可以在此处显示其他结果。

http://www.endyourif.com/custom-pagination-query-in-cakephp/ http://www.endyourif.com/custom-pagination-query-in-cakephp/

Answer for Custom Pagination in Cake PHP 回答Cake PHP中的自定义分页

A good example of when you would need this is if the underlying DB does not support the SQL LIMIT syntax. 如果底层数据库不支持SQL LIMIT语法,那么何时需要它的一个很好的例子。 This is true of IBM's DB2. IBM的DB2也是如此。 You can still use the CakePHP pagination by adding the custom query to the model. 您仍然可以通过将自定义查询添加到模型来使用CakePHP分页。

Should you need to create custom queries to generate the data you want to paginate, you can override the paginate() and paginateCount() model methods used by the pagination controller logic. 如果需要创建自定义查询以生成要分页的数据,则可以覆盖分页控制器逻辑使用的paginate()和paginateCount()模型方法。 You also need to override the core paginateCount(), this method expects the same arguments as Model::find('count'). 您还需要覆盖核心paginateCount(),此方法需要与Model :: find('count')相同的参数。 The example below uses some Postgres-specifc features, so please adjust accordingly depending on what database you are using. 下面的示例使用了一些Postgres特有的功能,因此请根据您使用的数据库进行相应调整。

Thanks see if this helps 谢谢看看这是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM