1

我的调用/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>

调用控制器:

<?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
}
4

3 回答 3

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
}

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

于 2014-05-25T13:17:54.893 回答
1

您示例中的正确调用应如下所示:

$this->set('调用', $this->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
}
于 2014-05-24T16:27:58.203 回答
0
<?php
App::uses('AppController', 'Controller');

class CallsController extends AppController {

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

}
于 2014-05-24T11:25:50.773 回答