CakeFest #3: July 9-12 2009 Berlin!

4.9.2 Pagination in Views

It's up to you to decide how to show records to the user, but most often this will be done inside HTML tables. The examples below assume a tabular layout, but the PaginatorHelper available in views doesn't always need to be restricted as such.

See the details on PaginatorHelper in the API.

As mentioned, the PaginatorHelper also offers sorting features which can be easily integrated into your table column headers:

// app/views/recipes/list_recipes.ctp
<table>
	<tr> 
		<th><?php echo $paginator->sort('ID', 'id'); ?></th> 
		<th><?php echo $paginator->sort('Title', 'title'); ?></th> 
	</tr> 
	   <?php foreach($data as $recipe): ?> 
	<tr> 
		<td><?php echo $recipe['Recipe']['id']; ?> </td> 
		<td><?php echo $recipe['Recipe']['title']; ?> </td> 
	</tr> 
	<?php endforeach; ?> 
</table> 
  1. // app/views/recipes/list_recipes.ctp
  2. <table>
  3. <tr>
  4. <th><?php echo $paginator->sort('ID', 'id'); ?></th>
  5. <th><?php echo $paginator->sort('Title', 'title'); ?></th>
  6. </tr>
  7. <?php foreach($data as $recipe): ?>
  8. <tr>
  9. <td><?php echo $recipe['Recipe']['id']; ?> </td>
  10. <td><?php echo $recipe['Recipe']['title']; ?> </td>
  11. </tr>
  12. <?php endforeach; ?>
  13. </table>

The links output from the sort() method of the PaginatorHelper allow users to click on table headers to toggle the sorting of the data by a given field.

It is also possible to sort a column based on associations:

<table>
	<tr> 
		<th><?php echo $paginator->sort('Title', 'title'); ?></th> 
		<th><?php echo $paginator->sort('Author', 'Author.name'); ?></th> 
	</tr> 
	   <?php foreach($data as $recipe): ?> 
	<tr> 
		<td><?php echo $recipe['Recipe']['title']; ?> </td> 
		<td><?php echo $recipe['Author']['name']; ?> </td> 
	</tr> 
	<?php endforeach; ?> 
</table> 
  1. <table>
  2. <tr>
  3. <th><?php echo $paginator->sort('Title', 'title'); ?></th>
  4. <th><?php echo $paginator->sort('Author', 'Author.name'); ?></th>
  5. </tr>
  6. <?php foreach($data as $recipe): ?>
  7. <tr>
  8. <td><?php echo $recipe['Recipe']['title']; ?> </td>
  9. <td><?php echo $recipe['Author']['name']; ?> </td>
  10. </tr>
  11. <?php endforeach; ?>
  12. </table>

The final ingredient to pagination display in views is the addition of page navigation, also supplied by the PaginationHelper.

<!-- Shows the page numbers -->
<?php echo $paginator->numbers(); ?>
<!-- Shows the next and previous links -->
<?php
	echo $paginator->prev('« Previous ', null, null, array('class' => 'disabled'));
	echo $paginator->next(' Next »', null, null, array('class' => 'disabled'));
?> 
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $paginator->counter(); ?>
  1. <!-- Shows the page numbers -->
  2. <?php echo $paginator->numbers(); ?>
  3. <!-- Shows the next and previous links -->
  4. <?php
  5. echo $paginator->prev('« Previous ', null, null, array('class' => 'disabled'));
  6. echo $paginator->next(' Next »', null, null, array('class' => 'disabled'));
  7. ?>
  8. <!-- prints X of Y, where X is current page and Y is number of pages -->
  9. <?php echo $paginator->counter(); ?>

The wording output by the counter() method can also be customized using special markers:

<?php
echo $paginator->counter(array(
	'format' => 'Page %page% of %pages%, showing %current% records out of
			 %count% total, starting on record %start%, ending on %end%'
)); 
?>
  1. <?php
  2. echo $paginator->counter(array(
  3. 'format' => 'Page %page% of %pages%, showing %current% records out of
  4. %count% total, starting on record %start%, ending on %end%'
  5. ));
  6. ?>

To pass all URL arguments to paginator functions, add the following to your view:

	$paginator->options(array('url' => $this->passedArgs));
  1. $paginator->options(array('url' => $this->passedArgs));

Route elements that are not named arguments should manually be merged with $this->passedArgs:

//for urls like http://www.example.com/en/controller/action
//that are routed as Router::connect('/:lang/:controller/:action/*', array(),array('lang'=>'ta|en'));
$paginator->options(array('url'=>array_merge(array('lang'=>$lang),$this->passedArgs)));
  1. //for urls like http://www.example.com/en/controller/action
  2. //that are routed as Router::connect('/:lang/:controller/:action/*', array(),array('lang'=>'ta|en'));
  3. $paginator->options(array('url'=>array_merge(array('lang'=>$lang),$this->passedArgs)));

Or you can specify which params to pass manually:

	$paginator->options(array('url' =>  array("0", "1")));
  1. $paginator->options(array('url' => array("0", "1")));