4

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

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

1 | 2 | 3 | 4 | ...

通过查看 options ,有一些选项可以更改分隔符,添加样式 css ..等的类。

我想要的是这样的分页:

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

有人对如何编码有想法?

编辑 :

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

我已经将我的添加到我CustomPaginatorHelper的 $ helpersof my 中Controller

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

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

编辑 2:

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

$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 添加到我的控制器的 $helpers 变量中,但我仍然得到同样的错误;有任何想法吗 ?

提前致谢

4

5 回答 5

4

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

不幸的是,没有办法使用 PaginatorHelper 实现您想要实现的目标。如果您想这样做,最好的方法是扩展 PaginatorHelper 类并覆盖该numbers()方法以返回您想要的内容。

我已经看过那个特定的方法,遗憾的是它不是很好 - 它有超过 100 行长!但是,我创建了一个子类 PaginatorHelper 并覆盖该方法的类。由于原始方法很长,因此需要大量复制和粘贴,因此我没有将其直接放在此答案中。

你可以在这里查看:https ://gist.github.com/2037902

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

于 2012-03-09T22:58:58.083 回答
1

你可以在这里显示答案

自定义查询分页

您还可以在此处显示另一个结果。

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

于 2012-03-20T12:23:41.650 回答
1

Cake PHP中自定义分页的答案

当底层数据库不支持 SQL LIMIT 语法时,您需要这样做的一个很好的例子。IBM 的 DB2 也是如此。您仍然可以通过将自定义查询添加到模型来使用 CakePHP 分页。

如果您需要创建自定义查询来生成要分页的数据,您可以覆盖分页控制器逻辑使用的 paginate() 和 paginateCount() 模型方法。您还需要覆盖核心 paginateCount(),此方法需要与 Model::find('count') 相同的参数。下面的示例使用了一些 Postgres 特定的功能,因此请根据您使用的数据库进行相应的调整。

谢谢看看这是否有帮助

于 2012-03-20T17:25:22.913 回答
1

我发现了一种更简单的方法来实现同样的目标。您会注意到 PaginationHelper 扩展了 AppHelper。因此,如果您将任何函数从 PaginatorHelper 复制到 AppHelper 调用并从 PaginationHelper 调用它,它将以相同的方式运行并且没有任何错误。

用法

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

代码

// /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;
    }

}
于 2013-08-24T16:26:43.330 回答
1

我发现了更简单的解决方案,不需要自定义代码,并且将采用 CakePHP 的方式,可能无法在低于版本的 CakePHP 2.0 中工作,因为我签入了 2.5...

对于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
);

现在我们可以随时覆盖这些默认值,只需options array()在您的情况下使用这些属性,例如:

    $this->Paginator->numbers(
                              array(
                                'separator' => ' - ',
                                'after' => '',
                                'before' => ''
    ));
于 2014-11-20T11:47:03.980 回答