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 ビュー ヘルパー クラスを拡張します。

以下のようにフォルダにファイル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;
    }
}

ファイルの元の関数を変更して、最初と最後の関数をオーバーライドするだけですlib\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 に答える