1

私のcalls/index.ctpに次のコードがあります。現時点では、すべてのレコードを 1 ページに表示しています。1 ページあたり 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
}

注 : ページネーションを適用する方法は 2 つあります。1 つはコンポーネントを使用する方法で、もう 1 つはコントローラーにある方法です。

于 2014-05-25T13:17:54.893 に答える
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 に答える