我是cakeHP的初学者,我不想在cakeHP中创建自定义分页。
函数$paginator->numbers() ;
,它显示的页码如下:
1 | 2 | 3 | 4 | ...
通过查看选项,有一些选项可以更改分隔符,添加样式为css的类。。等
我想要的是这样的分页:
1-20 21-40 41-60 61-80 ... >>
有人知道如何编码它吗?
编辑:
我已经在以下位置创建了自定义分页器助手:app/View/Helper/
,
我已经把我的CustomPaginatorHelper
添加到了Controller
的$helpers
中,如下所示:
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添加到我的Controller的$helpers变量中,但我仍然得到了同样的错误;有什么想法吗?
提前感谢
这里要知道的关键是有一个分页器组件(在控制器中使用)和一个分页辅助器(在视图中使用)。您使用的是PaginatorHelper类,它处理与分页相关联的元素的呈现。
不幸的是,使用PaginatorHelper无法实现您想要实现的目标。如果您想这样做,最好的方法是扩展PaginatorHelper类并重写numbers()
方法以返回您想要的内容。
我看了一下这种特殊的方法,遗憾的是它不太好——它有100多行长!然而,我已经创建了一个类,它将PaginatorHelper子类化并重写该方法。由于原始方法太长,需要大量的复制和粘贴,因此我没有直接将其放在这个答案中。
您可以在此处查看:https://gist.github.com/2037902
您还需要将CustomPaginator
添加到控制器中的辅助对象列表中。
您可以在此处显示答案
自定义查询分页
您也可以在此处显示另一个结果。
http://www.endyourif.com/custom-pagination-query-in-cakephp/
Cake PHP中自定义分页的答案
如果底层数据库不支持SQLLIMIT语法,那么就是一个很好的例子。IBM的DB2就是这样。您仍然可以通过向模型中添加自定义查询来使用CakePHP分页。
如果需要创建自定义查询来生成要分页的数据,可以覆盖分页控制器逻辑使用的paginate()和paginateCount()模型方法。您还需要重写核心paginateCount(),此方法需要与Model::find('count')相同的参数。下面的例子使用了一些Postgres特有的功能,所以请根据您使用的数据库进行相应的调整。
谢谢,看看这是否有助于
我发现了实现同样目标的更简单的方法。您会注意到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;
}
}
我发现了更简单的解决方案,不需要自定义代码,并且将以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' => ''
));