如何在编写自己的分页模板时避免使用分页符行项

时间:2016-03-13 14:38:07

标签: php cakephp pagination cakephp-3.0

情况

Cake 3.2.4

我想要什么

<a href="#" class="pagination__item pagination__prev">&larr;</a>
<a href="#" class="pagination__item is-active">1</a>
<a href="#" class="pagination__item">2</a>
<a href="#" class="pagination__item">3</a>
<a href="#" class="pagination__item">4</a>
<a href="#" class="pagination__ellipsis">...</a>
<a href="#" class="pagination__item">10</a>
<a href="#" class="pagination__item pagination__next">&rarr;</a>

分页链接如上所示,正确填写了href值而不是#

我尝试了什么

<?= $this->Paginator->prev('<') ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next('>') ?>

然后在AppView.php里面

public function initialize()
{
    $this->loadHelper('Paginator', ['templates' => 'MyPlugin.paginator-templates']);
}

然后在MyPlugin / config / paginator-templates.php

<?php

return [
    'number' => '<a href="{{url}}" class="pagination__item">{{text}}</a>',
    'current' => '<a href="{{url}}" class="pagination__item is-active">{{text}}</a>',
    'nextActive' => '<a href="{{url}}" class="pagination__item pagination__prev">{{text}}</a>',
    'prevActive' => '<a href="{{url}}" class="pagination__item pagination__next">{{text}}</a>',
    'ellipsis' => '<a href="{{url}}" class="pagination__ellipsis">{{text}}</a>'
];

发生了什么

我得到以下

<div class="pagination">
    <li class="prev disabled"><a href="" onclick="return false;">&lt;</a></li>      
    <li class="active"><a href="">1</a></li>
    <li><a href="/gallery?page=2">2</a></li>
    <li class="next"><a rel="next" href="/gallery?page=2">&gt;</a></li>    
  </div>

如何摆脱额外的li元素?

我没想到他们。我想通过使用我自己的paginator-templates,我将解决这个问题。

更新

当我将以下行添加到模板

<?php 
debug(get_class($this));

debug($this->Paginator->config('templates'));
?>

我回来了

/src/Template/Products/index.ctp (line 1)
'App\View\AppView'
/src/Template/Products/index.ctp (line 3)
'MyPlugin.paginator-templates'

因此调试行正如预期的那样。怎么了?

1 个答案:

答案 0 :(得分:2)

以下是分页的示例

<div class="paging" style="  background: none repeat scroll 0 0 #60C8F2;
    float: right;
    padding: 0 10px;
    text-align: left;
    width: auto;">
<?php
$this->Paginator->options(array(
    'url' => array_merge(array(
        'controller' => $this->params['controller'],
        'action' => $this->params['action'],
    ) , $this->params['pass'], $this->params['named'])
));

echo $this->Paginator->prev('' . '' , array(
    'class' => 'prev',
    'escape' => false
) , null, array(
    'tag' => 'span',
    'escape' => false,
    'class' => 'prev'
)), "\n";
echo $this->Paginator->numbers(array(
    'modulus' => 2,
    'skip' => '',
    'separator' => " \n",
    'before' => null,
    'after' => null,
    'escape' => false
));
echo $this->Paginator->next('' . '', array(
    'class' => 'next',
    'escape' => false
) , null, array(
    'tag' => 'span',
    'escape' => false,
    'class' => 'next'
)), "\n";
?>
</div>

将此代码放入元素pagination.ctp

调用控制器中的组件

var $components = array('Paginator');

将分页调用到您的控制器

 $this->paginate = array(
            'limit' => 10,
            'order' => array(
            'User.created' => 'desc')

        );
        $results = $this->paginate('User');

在ctp文件中调用分页

<?php echo $this->element('pagination'); ?>