【问题标题】:CakePHP Paginator, how to make current page an anchorCakePHP Paginator,如何使当前页面成为锚点
【发布时间】:2013-08-01 22:12:59
【问题描述】:

在我看来,我正在使用此代码:

$numbers = $this->Paginator->numbers(array(
    'separator'     => '',
    'tag'           => 'li',
    'currentClass'  => 'active'
));

哪个输出:

<li class="active">1</li>
<li><a href="/controller/action/page:2">2</a></li>
<li><a href="/controller/action/page:3">3</a></li>

这很好用,我唯一的问题是当前页面不是链接。是否可以将当前页面设为链接?

感谢阅读。

【问题讨论】:

  • 您使用的是哪个 cakephp 版本??
  • 尝试将分隔符设为 false 并尝试
  • 我尝试将分隔符设置为 false,但结果相同,我使用的是 cake 2.0.5。
  • 仅供参考:currentClass 选项仅适用于 cake 2.1 及更高版本。所以这实际上是行不通的。我在下面为遇到此问题的任何人添加了我的解决方案。

标签: php cakephp pagination


【解决方案1】:

我像 Dave 建议的那样实现了一个类扩展,但是我没有从原始类中复制所有代码,而是使用了一个字符串替换方法,这样,如果我更新 CakePHP 核心库,这应该会非常优雅地失败,其中因为从原始 Helper 复制所有代码可能会导致功能丢失、错误修复等。这是我实现的类:

<?php

class AppPaginatorHelper extends PaginatorHelper
{
    public function numbers($options = array()) {
        $output = parent::numbers($options);

        // get the current page number, and create a link with it
        $current = $this->current();
        $currentLink = $this->link($current, array('page' => $current));

        // if you're using cake pre 2.1 you cannot change the current class with
        // the options array, so it will always be "current"
        $find = "<li class=\"current\">{$current}</li>";
        $replace = "<li class=\"active\">{$currentLink}</li>"; 

        $output = str_replace($find, $replace, $output);

        return $output;
    }
}

【讨论】:

    【解决方案2】:

    有一个 PR 可以让它发挥作用。 不幸的是,它只会在 2.3 版本中发生。

    在这里你可以找到 PR https://github.com/cakephp/cakephp/pull/900

    在这里你可以找到讨论 http://cakephp.lighthouseapp.com/projects/42648/tickets/2892-paginator-helper-numbers-is-a-bit-counter-intuitive-enhancement-included

    【讨论】:

      【解决方案3】:

      你可以使用下一段代码来制作你想要的东西:

      <?php echo $this->Paginator->numbers(array('separator' => '','tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'active')); ?>
      

      仅适用于 CakePHP 2.3+

      【讨论】:

        【解决方案4】:

        简单回答:

        我不相信Paginator Helper 可以使用它。

        如果你只想要一个指向当前页面的链接,但在编号的链接中不需要它,你可以使用

        echo $this->Html->link($this->Paginator->counter('{:current}'), 'yourLinkHere');
        

        但这并不是非常有用,因为您依赖 Paginator Helper 来为您处理其余的链接。

        扩展答案/可能性

        您可以使用以下类似的方式扩展 PaginatorHelper。基本上,我只是删除了检查以查看它是否是当前页码。然后你必须使用MyPaginatorHelper 来构建链接。这也会使其忽略currentClass 选项...等。但是 - 通过对代码进行更多的调整,你可以让它做同样的事情,但也建立一个链接,而不是仅仅删除 IF 检查。

        class MyPaginatorHelper extends PaginatorHelper {
            public function numbers($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'
            );
            $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']
            );
        
            $out = '';
        
        
                $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') + 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;
                }
                $out .= $this->Html->tag($tag, $params['page'], 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') + array('before' => $separator));
                    }
                }
        
            }
        
            return $out;
        }
        }
        

        【讨论】:

        • 感谢您确认我对 PaginatorHelper 的怀疑。这似乎是一个简单的选择,保留所有链接,我想也许从事 CakePHP 工作的人会在其中添加它。我最终创建了一个在 PaginatorHelper 中扩展的类,但实现方式略有不同。
        【解决方案5】:

        我找到了一个很好的解决方案并分享给你:)

        <div class="pagination pagination-large">
            <ul>
                    <?php
                        echo $this->Paginator->prev(__('prev'), array('tag' => 'li'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
                        echo $this->Paginator->numbers(array('separator' => '','currentTag' => 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
                        echo $this->Paginator->next(__('next'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
                    ?>
                </ul>
        </div>
        

        【讨论】: