Cake PHP Pagination更改以限制每页的记录

时间:2014-05-24 10:02:53

标签: cakephp paginator

我的调用/ index.ctp中有以下代码。目前,它在一个页面中显示所有记录。我想限制每页20条记录。当我烘焙项目时,提供了分页,但我仍然是初学者,并且不知道如何改变分页。有人可以帮忙吗?

呼叫/ index.ctp:

<div class="callsIndex">
    <h2><?php echo __('Call Details'); ?>   </h2>
    <div class="bottomButtonnew"><?php echo $this->Html->link(__('Add Calls'), array('action' => 'add')); ?></div>


    <table cellpadding="0" cellspacing="0">
        <tr>

            <th><?php echo $this->Paginator->sort('Call Date'); ?></th>
            <th><?php echo $this->Paginator->sort('Call Time'); ?></th>
            <th><?php echo $this->Paginator->sort('Comments'); ?></th>
            <th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
            <th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
            <th><?php echo $this->Paginator->sort('Company Name'); ?></th>
            <th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
            <th class="actions"><?php echo __(''); ?></th>
        </tr>
        <?php foreach ($calls as $call): ?>
            <tr>

                <td><?php echo date("d-m-Y", strtotime($call['Call']['call_date'])); ?>&nbsp;</td>
                <td><?php echo h($call['Call']['call_time']); ?>&nbsp;</td>
                <td><?php echo h($call['Call']['comments']); ?>&nbsp;</td>
                <td><?php echo date("d-m-Y", strtotime($call['Call']['next_call_date'])); ?>&nbsp;</td>
                <td>
                    <?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>

                </td>
                <td>
                    <?php echo $this->Html->link($call['Companies']['company_name'], array('controller' => 'companies', 'action' => 'view', $call['Companies']['id'])); ?>
                </td>
                <td>
                    <?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
                </td>
                <td class="actions">


                    <?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
    <p>
        <?php
        echo $this->Paginator->counter(array(
            'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total')
        ));
        ?>  </p>
    <div class="paging">
        <?php
        echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('separator' => ''));
        echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
        ?>
    </div>
    <br>

</div>

callsController:

<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {

public $components = array('Paginator');
public $paginate = array(
        'limit' => 10
    );

public function index() {
        $userid=$this->Session->read('User.userid');
        if(isset($userid)&&$userid!=null)
        {
            $this->Call->recursive = 0;
            $this->set('calls', $this->Paginator->paginate());
            $result=$this->Call->getcalls($userid);
            $this->set('result', $result);
        }
        else{
            $this->set('loggedout',"loggedout");
            $this->render("../Pages/home");
            $this->layout = '../Pages/home';
        }


    }
//some code
}

3 个答案:

答案 0 :(得分:5)

您可以使用分页组件轻松完成。您应该在AppController中执行此操作。在AppController中,您可以键入以下代码

Public $components =array('paginator');

    public function beforeFilter()
    {
       parent::beforeFilter();
       $this->Paginator->settings=array(
              'limit'=>10
       );
    } 

比你索引方法只需改变这一行

$this->set('calls', $this->Paginator->paginate());

我认为现在可以正常工作。这段代码适用于所有控制器。但是如果你只想更改你的CallsController。做这个代码。

<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {
public $components = array('Paginator');

public function index() {

      $this->Paginator->settings = array(
        'limit' => 10
    );
        $userid=$this->Session->read('User.userid');
        if(isset($userid)&&$userid!=null)
        {
            $this->Call->recursive = 0;
            $this->set('calls', $this->Paginator->paginate());
            $result=$this->Call->getcalls($userid);
            $this->set('result', $result);
        }
        else{
            $this->set('loggedout',"loggedout");
            $this->render("../Pages/home");
            $this->layout = '../Pages/home';
        }


    }
//some code
}

注意:有两种方法可以应用分页,一种是使用另一种方法,另一种是在控制器中。

答案 1 :(得分:1)

您的示例中的正确调用应该是这样的:

$ this-&gt; set('calls',$ this-&gt; paginate());

所以你的代码是:

<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {

public $components = array('Paginator');
public $paginate = array(
        'limit' => 10 //Here can you change the default limit
    );

public function index() {
        $userid=$this->Session->read('User.userid');
        if(isset($userid)&&$userid!=null)
        {
            $this->Call->recursive = 0;
            $this->set('calls', $this->paginate());
            $result=$this->Call->getcalls($userid);
            $this->set('result', $result);
        }
        else{
            $this->set('loggedout',"loggedout");
            $this->render("../Pages/home");
            $this->layout = '../Pages/home';
        }


    }
//some code
}

答案 2 :(得分:0)

<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {

public $components = array('Paginator');
public $paginate = array(
    'limit' => 20
);

}