【问题标题】:Custom Pagination in Cake PHPCake PHP 中的自定义分页
【发布时间】:2014-11-20 19:47:03
【问题描述】:

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

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

提前致谢

【问题讨论】:

  • 你为什么不在 PHP 中标记你的问题
  • 我已经标记了它:),我忘记了:(

标签: php cakephp-2.0 paginator


【解决方案1】:

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

很遗憾,PaginatorHelper 无法实现您想要实现的目标。如果您想这样做,最好的方法是扩展 PaginatorHelper 类并覆盖 numbers() 方法以返回您想要的内容。

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

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

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

【讨论】:

  • 谢谢乔纳森的这个提示,我会尝试并告诉你结果:) +1 解释
  • 我已经在 gitHub 中看到了分页器,但问题是我遇到了同样的错误:他找不到 PaginatorHelper,我不知道为什么:致命错误:Class '在第 3 行的 /Applications/MAMP/htdocs/QRCode/app/View/Helper/CustomPaginatorHelper.php 中找不到 PaginatorHelper'
  • 我已经添加了它,我不再收到此错误,请参阅我的编辑
  • 好的,如果没有看到代码,我不确定我还能提供什么帮助。问题是没有加载帮助程序。我知道帮助程序有效,因为我用我的代码对其进行了测试:) - 我认为这只是仔细检查帮助程序文件位置并且正确的文件正在使用它的情况。祝你好运!
  • 如果您可以在 Posts 的简单示例中使用 customPaginator 并将其发送给我,我将不胜感激,我也不知道为什么这不起作用,我已经添加了我的 customPaginator到 $helpers 并且我遵循了所有步骤,但我在这里:(
【解决方案2】:

你可以在这里显示答案

Custom-Query-Pagination

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

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

【讨论】:

    【解决方案3】:

    Cake PHP 中自定义分页的答案

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

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

    谢谢see if this helps

    【讨论】:

      【解决方案4】:

      我发现了实现相同目标的更简单方法。您会注意到 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;
          }
      
      }
      

      【讨论】:

        【解决方案5】:

        我找到了更简单的解决方案,不需要自定义代码,并且将采用 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' => ''
            ));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-27
          • 2012-09-18
          • 2012-11-12
          • 1970-01-01
          • 2022-10-14
          相关资源
          最近更新 更多