0

我有这个代码

<div class="paging">
<?php
    echo $this->Paginator->first('<<', array('class' => 'first'));
    echo $this->Paginator->prev('<', array(), null, array('class' => 'prev disabled'));
    echo '<span class="numbers">';
    echo $this->Paginator->numbers();
    echo '</span>';
    echo $this->Paginator->next('>', array(), null, array('class' => 'next disabled'));
    echo $this->Paginator->last('>>', array('class' => 'last'));
?>
</div>

但问题是,如果我在第一页,“<<”链接将不会显示。我认为它是由 cakephp 设计的,它是这样的。无论如何都显示第一页和最后一页的跳转链接,即使我在第一页或最后一页?

4

1 回答 1

2

您可以通过扩展 Paginator 视图 Helper 类来做到这一点。

PaginatorExtHelper.php在您的文件夹中创建文件View/Helper/,如下所示。

<?php
App::uses('PaginatorHelper', 'View/Helper');

class PaginatorExtHelper extends PaginatorHelper {

    public function first($first = '<< first', $options = array()) {
        $options = array_merge(
            array(
                'tag' => 'span',
                'after' => null,
                'model' => $this->defaultModel(),
                'separator' => ' | ',
                'ellipsis' => '...',
                'class' => null
            ),
        (array)$options);

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

        if ($params['pageCount'] <= 1) {
            return false;
        }
        extract($options);
        unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);

        $out = '';

        if (is_int($first) && $params['page'] >= $first) {
            if ($after === null) {
                $after = $ellipsis;
            }
            for ($i = 1; $i <= $first; $i++) {
                $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
                if ($i != $first) {
                    $out .= $separator;
                }
            }
            $out .= $after;
        } elseif ($params['page'] >= 1 && is_string($first)) {
            $options += array('rel' => 'first');
            if($params['page'] == 1){
                $out = $this->Html->tag($tag, $first, compact('class')) . $after;
            }else{
                $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
            }
        }
        return $out;
    }

    public function last($last = 'last >>', $options = array()) {
        $options = array_merge(
            array(
                'tag' => 'span',
                'before' => null,
                'model' => $this->defaultModel(),
                'separator' => ' | ',
                'ellipsis' => '...',
                'class' => null
            ),
        (array)$options);

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

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

        extract($options);
        unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);

        $out = '';
        $lower = $params['pageCount'] - $last + 1;

        if (is_int($last) && $params['page'] <= $lower) {
            if ($before === null) {
                $before = $ellipsis;
            }
            for ($i = $lower; $i <= $params['pageCount']; $i++) {
                $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
                if ($i != $params['pageCount']) {
                    $out .= $separator;
                }
            }
            $out = $before . $out;
        } elseif ($params['page'] <= $params['pageCount'] && is_string($last)) {
            $options += array('rel' => 'last');

            if($params['page'] == $params['pageCount']){
                $out = $before . $this->Html->tag(
                    $tag, $last, compact('class')
                );
            }else{          
                $out = $before . $this->Html->tag(
                    $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
                );
            }
        }
        return $out;
    }
}

只需通过更改文件的原始函数来覆盖函数 first 和 lastlib\Cake\View\Helper\PaginatorHelper.php

你可以像下面这样使用这个助手

<div class="paging">
<?php
    echo $this->PaginatorExt->first('<<', array('class' => 'first'));
    echo $this->PaginatorExt->prev('<', array(), null, array('class' => 'prev disabled'));
    echo '<span class="numbers">';
    echo $this->PaginatorExt->numbers();
    echo '</span>';
    echo $this->PaginatorExt->next('>', array(), null, array('class' => 'next disabled'));
    echo $this->PaginatorExt->last('>>', array('class' => 'last'));
?>
</div>

它对我有用。

于 2013-10-10T07:22:22.447 回答