【问题标题】:How to avoid paginator line items when I write my own pagination templates编写自己的分页模板时如何避免分页器行项目
【发布时间】:2016-06-28 13:08:43
【问题描述】:

情况

蛋糕 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>

分页链接如上所示,正确填写了 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 元素?

我没想到他们。我想通过使用我自己的分页器模板,我会解决这个问题。

更新

当我在模板中添加以下行时

<?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'

所以调试行符合预期。怎么了?

【问题讨论】:

  • 对我来说很好。对我来说,这看起来好像没有实际应用您的模板。您确定您的 AppView 类实际上正在被使用吗?
  • 如何检查 Appview 是否被应用?我读了book.cakephp.org/3.0/en/views.html#the-app-view 什么都没看到
  • 您可以在视图模板中调试当前类,例如debug(get_class($this))。还要检查帮助程序是否设置了您的模板debug($this-&gt;Paginator-&gt;config('templates'))
  • 所以我将两个调试行都放在了模板文件中?
  • 我查过了。我得到了完全符合我预期的结果。那么接下来呢?

标签: php cakephp pagination cakephp-3.0


【解决方案1】:

这里是分页的例子

<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'); ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多