尽管使用了paginator helper和分页Cakephp 3.x,但分页显示了所有结果

时间:2016-11-14 11:59:43

标签: cakephp controller pagination paginator

所以我是 Cakephp 3.x 的新手,我正在尝试对一组数据进行分页,但问题是所有条目都在显示,底部的导航栏也是显示,我猜它工作正常。

以下是我的控制器。

public $paginate = ['limit' => 2,'order' => ['sample.posts' => 'asc']];

public function initialize()
{
    parent::initialize();
    $this->layout = 'frontend';
    $this->loadComponent('Paginator');
    $this->loadComponent('Flash'); // Include the FlashComponent
}

public function index()
{
    $this->set('Posts', $this->paginate());
    $posts = $this->Posts->find('all');
    $this->set(compact('posts'));
}

这是我的模板文件。

<table>
        <tr>paginate
            <th><?php echo $this->Paginator->sort('ID', 'id'); ?></th>
            <th><?php echo $this->Paginator->sort('Title', 'title'); ?></th>
        </tr>
           <?php foreach($posts as $post): ?>
        <tr>
            <td><?php echo $post['id'] ?> </td>
            <td><?php echo $post['title'] ?> </td>
        </tr>
    <?php endforeach; ?>
</table>

<?php echo $this->Paginator->numbers(); ?>  
<?php echo $this->Paginator->prev('« Previous', array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', array('class' => 'disabled')); ?>   
<?php echo $this->Paginator->counter(); ?>

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

你的索引函数应该有一个paginate函数,你应该给$ posts作为参数。

       public function index()
       {
           $posts = $this->Posts->find('all');
           $posts = $this->paginate($posts);
           $this->set(compact('posts'));
       }