FAQ
Pagination
# 1 > Ensure your $components has 'Paginator'
in app->Controller->AppController, add the following
public $components = array(
//other components are here
'Paginator'
);
# 2 > Pagination in Action
- in App->Controller->YourController
- Add the following to the action
function actionName(){
$conditions = array('AND' => array(
array('Model.group_id' => $this->groupId()),
));
$contain = array('User', 'Group' =>array());
//by default we need a limit to init the pagination
$this->paginate = array(
'conditions' => $conditions,
'contain' => $contain,
'order' => 'Model.name ASC',
'limit' => 100
);
$records = $this->paginate('Model');
$this->set('records', $records);
# 3 > Pagination in View
- in app->View->action_name.ctp,
- To sort the data, add the following
<table>
<tr>
<th><?= $this->Paginator->sort('DBfieldName', $this->Translate->word('PublicName')); ?></th>
(Example: <?php echo $this->Paginator->sort('name', 'Name'); ?>
<th>--</th>
<th>--</th>
</tr>
<?php foreach ($records as $record): ?>
// Data goes here
<?= $record['Model']['name']; ?>
<?php endforeach; ?>
<?= $this->Element('prev_next'); ?>- Create an Element - in app/View/Element add file prev_next.ctp (app/View/Element/prev_next.ctp)
Add the following code to prev_next.ctp file
<div class="previous_next">
<< <?php echo $this->paginator->prev($this->Translate->word('Previous'), array(
'class' => 'PrevPg'
), null, array(
'class' => 'disabled'
)); ?>
<?php
echo $this->paginator->counter(array(
'format' => $this->Translate->word('of')
));
?>
<?php
echo $this->paginator->counter(array(
'format' => $this->Translate->word('pages')
));
?>
<?php
echo $this->paginator->counter(array(
'format' => $this->Translate->word('showing')
));
?>
<?php
echo $this->paginator->counter(array(
'format' => '%current%'
));
?>
<?php
echo $this->paginator->counter(array(
'format' => $this->Translate->word('of')
));
?>
<?php
echo $this->paginator->counter(array(
'format' => '%count%'
));
?>)
<?php echo $this->paginator->next($this->Translate->word('Next'), array(
'class' => 'NextPg'
), null, array(
'class' => 'disabled'
));
?> >>
</div>