php - 当我编写自己的分页模板时如何避免分页器行项目

标签 php cakephp pagination cakephp-3.0

情况

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

因此调试行符合预期。怎么了?

最佳答案

这是分页的示例

<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

调用 Controller 中的组件

var $components = array('Paginator');

将分页调用到 Controller 中

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

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

并在ctp文件中调用分页

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

关于php - 当我编写自己的分页模板时如何避免分页器行项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971552/

相关文章:

php - 检查xml文件是否为空

php - 使用 PhpStorm 在 CakePHP 中删除插件后出现 "Member has private access error"

php - 如何使用 PHP 仅获取 CSV 文件的第二列?

PHP 错误记录。如何预防?

用于更新多行(如标记)的 PHP AJAX 数据网格?

Mysql分页

php - 正确使用 Laravel 的分页方法?

php - Codeigniter 分页链接按降序/倒序排列?

javascript - 如何使用 ajax 响应将 'selected' 设置为具有匹配值的下拉菜单?

php - CakePHP 3 : Auth->identify() always let a valid username in without checking the password