Use PaginatorHelper in Cells CakePhp 3

。_饼干妹妹 提交于 2019-12-24 19:34:01

问题


Hi I'm trying to use Paginator within a Cell the Paginator works rigth but the PaginatorHelper in the Template of the cell just doesn't work.

I'm using CakePhp 3.5

Here is my code

src/View/Cell/FinderCell.php

namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
class FinderCell extends Cell {

    public function display() {
        $this->loadModel('Pokemon');
        $paginador = new Paginator();

        $pokemons = $paginador->paginate(
            $this->Pokemon->find()
        );

        $this->set(compact('pokemons'));
    }
}

src/Template/Cell/Finder/display.ctp

<?php 
foreach ($pokemons as $pokemon) :
 ?>
<div class="tipo form large-2 medium-2 columns content">
    <?php echo $this->Html->image($pokemon->pokemon_image) ?>
</div>
<?php endforeach; ?>

<div class="paginator">
    <ul class="pagination">
        <?= $this->Paginator->prev('<< Anterior') ?>
        <?= $this->Paginator->numbers() ?>
        <?= $this->Paginator->next('Siguiente >>') ?>
    </ul>
    <p><?= $this->Paginator->counter() ?></p>
</div>

I get this


回答1:


If you want to use the helper, then you need to populate either the request object with the pagination parameters, which is what the paginator component would normally do for you:

// ...

$this->request = $this->request->withParam(
    'paging',
    $paginator->getPagingParams() + (array)$this->request->getParam('paging')
);

$this->set(compact('pokemons'));

or the paginator helper, which in turn sets the parameters on the request object:

// ...

$pagingParams = $paginator->getPagingParams();

$this->set(compact('pokemons', 'pagingParams'));
$this->Paginator->options(['paging' => $pagingParams]);

See also

  • API > \Cake\Datasource\PaginatorInterface::getPagingParams()
  • API > \Cake\View\Helper\PaginatorHelper::options()


来源:https://stackoverflow.com/questions/47213707/use-paginatorhelper-in-cells-cakephp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!