The Pagination helper is used to output pagination controls such as page numbers and next/previous links. It works in tandem with PaginatorComponent.
See also Pagination for information on how to create paginated datasets and do paginated queries.
| Parameters: |
|
|---|
Generates a sorting link. Sets named or querystring parameters for the sort and direction. Links will default to sorting by asc. After the first click, links generated with sort() will handle direction switching automatically. Link sorting default by ‘asc’. If the resultset is sorted ‘asc’ by the specified key the returned link will sort by ‘desc’.
Accepted keys for $options:
Assuming you are paginating some posts, and are on page one:
echo $this->Paginator->sort('user_id');
Output:
<a href="/posts/index/page:1/sort:user_id/direction:asc/">User Id</a>
You can use the title parameter to create custom text for your link:
echo $this->Paginator->sort('user_id', 'User account');
Output:
<a href="/posts/index/page:1/sort:user_id/direction:asc/">User account</a>
If you are using HTML like images in your links remember to set escaping off:
echo $this->Paginator->sort('user_id', '<em>User account</em>', array('escape' => false));
Output:
<a href="/posts/index/page:1/sort:user_id/direction:asc/"><em>User account</em></a>
The direction option can be used to set the default direction for a link. Once a link is active, it will automatically switch directions like normal:
echo $this->Paginator->sort('user_id', null, array('direction' => 'desc'));
Output:
<a href="/posts/index/page:1/sort:user_id/direction:desc/">User Id</a>
Gets the current direction the recordset is sorted.
Gets the current key by which the recordset is sorted.
Returns a set of numbers for the paged result set. Uses a modulus to decide how many numbers to show on each side of the current page By default 8 links on either side of the current page will be created if those pages exist. Links will not be generated for pages that do not exist. The current page is also not a link.
Supported options are:
before Content to be inserted before the numbers.
after Content to be inserted after the numbers.
model Model to create numbers for, defaults to PaginatorHelper::defaultModel().
modulus how many numbers to include on either side of the current page, defaults to 8.
separator Separator content defaults to `` | ``
tag The tag to wrap links in, defaults to ‘span’.
first Whether you want first links generated, set to an integer to define the number of ‘first’ links to generate. Defaults to false. If a string is set a link to the first page will be generated with the value as the title:
echo $this->Paginator->numbers(array('first' => 'First page'));
last Whether you want last links generated, set to an integer to define the number of ‘last’ links to generate. Defaults to false. Follows the same logic as the first option. There is a last()` method to be used separately as well if you wish.
ellipsis Ellipsis content, defaults to ‘...’
class The classname used on the wrapping tag.
currentClass The classname to use on the current/active link. Defaults to current.
currentTag Tag to use for current page number, defaults to null. This allows you to generate for example Twitter Bootstrap like links with the current page number wrapped in extra ‘a’ or ‘span’ tag.
While this method allows a lot of customization for its output. It is also ok to just call the method without any params.:
echo $this->Paginator->numbers();
Using the first and last options you can create links to the beginning and end of the page set. The following would create a set of page links that include links to the first 2 and last 2 pages in the paged results:
echo $this->Paginator->numbers(array('first' => 2, 'last' => 2));
New in version 2.1: The currentClass option was added in 2.1.
New in version 2.3: The currentTag option was added in 2.3.
In addition to generating links that go directly to specific page numbers, you’ll often want links that go to the previous and next links, first and last pages in the paged data set.
| Parameters: |
|
|---|
Generates a link to the previous page in a set of paged records.
$options and $disabledOptions supports the following keys:
A simple example would be:
echo $this->Paginator->prev(' << ' . __('previous'), array(), null, array('class' => 'prev disabled'));
If you were currently on the second page of posts, you would get the following:
<span class="prev"><a rel="prev" href="/posts/index/page:1/sort:title/order:desc"><< previous</a></span>
If there were no previous pages you would get:
<span class="prev disabled"><< previous</span>
You can change the wrapping tag using the tag option:
echo $this->Paginator->prev(__('previous'), array('tag' => 'li'));
Output:
<li class="prev"><a rel="prev" href="/posts/index/page:1/sort:title/order:desc">previous</a></li>
You can also disable the wrapping tag:
echo $this->Paginator->prev(__('previous'), array('tag' => false));
Output:
<a class="prev" rel="prev" href="/posts/index/page:1/sort:title/order:desc">previous</a>
Changed in version 2.3: For methods: PaginatorHelper::prev() and PaginatorHelper::next() it is now possible to set the tag option to false to disable the wrapper. New options disabledTag has been added.
If you leave the $disabledOptions empty the $options parameter will be used. This can save some additional typing if both sets of options are the same.
This method is identical to prev() with a few exceptions. It creates links pointing to the next page instead of the previous one. It also uses next as the rel attribute value instead of prev
Returns a first or set of numbers for the first pages. If a string is given, then only a link to the first page with the provided text will be created:
echo $this->Paginator->first('< first');
The above creates a single link for the first page. Will output nothing if you are on the first page. You can also use an integer to indicate how many first paging links you want generated:
echo $this->Paginator->first(3);
The above will create links for the first 3 pages, once you get to the third or greater page. Prior to that nothing will be output.
The options parameter accepts the following:
This method works very much like the first() method. It has a few differences though. It will not generate any links if you are on the last page for a string values of $last. For an integer value of $last no links will be generated once the user is inside the range of last pages.
Gets the current page of the recordset for the given model:
// Our url is: http://example.com/comments/view/page:3
echo $this->Paginator->current('Comment');
// Output is 3
Returns true if the given result set is not at the last page.
Returns true if the given result set is not at the first page.
Returns true if the given result set has the page number given by $page.
Returns a counter string for the paged result set. Using a provided format string and a number of options you can create localized and application specific indicators of where a user is in the paged data set.
There are a number of options for counter(). The supported ones are:
format Format of the counter. Supported formats are ‘range’, ‘pages’ and custom. Defaults to pages which would output like ‘1 of 10’. In the custom mode the supplied string is parsed and tokens are replaced with actual values. The available tokens are:
You could also supply only a string to the counter method using the tokens available. For example:
echo $this->Paginator->counter(
'Page {:page} of {:pages}, showing {:current} records out of
{:count} total, starting on record {:start}, ending on {:end}'
);
Setting ‘format’ to range would output like ‘1 - 3 of 13’:
echo $this->Paginator->counter(array(
'format' => 'range'
));
separator The separator between the actual page and the number of pages. Defaults to ‘ of ‘. This is used in conjunction with ‘format’ = ‘pages’ which is ‘format’ default value:
echo $this->Paginator->counter(array(
'separator' => ' of a total of '
));
model The name of the model being paginated, defaults to PaginatorHelper::defaultModel(). This is used in conjunction with the custom string on ‘format’ option.
| Parameters: |
|
|---|
Sets all the options for the Paginator Helper. Supported options are:
url The url of the paginating action. url has a few sub options as well:
The above mentioned options can be used to force particular pages/directions. You can also append additional url content into all urls generated in the helper:
$this->Paginator->options(array(
'url' => array(
'sort' => 'email', 'direction' => 'desc', 'page' => 6,
'lang' => 'en'
)
));
The above adds the en route parameter to all links the helper will generate. It will also create links with specific sort, direction and page values. By default PaginatorHelper will merge in all of the current pass and named parameters. So you don’t have to do that in each view file.
escape Defines if the title field for links should be HTML escaped. Defaults to true.
update The CSS selector of the element to update with the results of AJAX pagination calls. If not specified, regular links will be created:
$this->Paginator->options(array('update' => '#content'));
This is useful when doing Ajax Pagination. Keep in mind that the value of update can be any valid CSS selector, but most often is simpler to use an id selector.
model The name of the model being paginated, defaults to PaginatorHelper::defaultModel().
Normally Pagination in CakePHP uses Named parameters. There are times you want to use GET parameters instead. While the main configuration option for this feature is in PaginatorComponent, you have some additional control in the view. You can use options() to indicate that you want other named parameters to be converted:
$this->Paginator->options(array('convertKeys' => array('your', 'keys', 'here')));
By default the PaginatorHelper uses JsHelper to do ajax features. However, if you don’t want that and want to use a custom helper for ajax links, you can do so by changing the $helpers array in your controller. After running paginate() do the following:
// In your controller action.
$this->set('posts', $this->paginate());
$this->helpers['Paginator'] = array('ajax' => 'CustomJs');
Will change the PaginatorHelper to use the CustomJs for ajax operations. You could also set the ‘ajax’ key to be any helper, as long as that class implements a link() method that behaves like HtmlHelper::link()
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/View/Posts/index.ctp
<table>
<tr>
<th><?php echo $this->Paginator->sort('id', 'ID'); ?></th>
<th><?php echo $this->Paginator->sort('title', 'Title'); ?></th>
</tr>
<?php foreach ($data as $recipe): ?>
<tr>
<td><?php echo $recipe['Recipe']['id']; ?> </td>
<td><?php echo h($recipe['Recipe']['title']); ?> </td>
</tr>
<?php endforeach; ?>
</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 $this->Paginator->sort('title', 'Title'); ?></th>
<th><?php echo $this->Paginator->sort('Author.name', 'Author'); ?></th>
</tr>
<?php foreach ($data as $recipe): ?>
<tr>
<td><?php echo h($recipe['Recipe']['title']); ?> </td>
<td><?php echo h($recipe['Author']['name']); ?> </td>
</tr>
<?php endforeach; ?>
</table>
The final ingredient to pagination display in views is the addition of page navigation, also supplied by the PaginationHelper:
// Shows the page numbers
echo $this->Paginator->numbers();
// Shows the next and previous links
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
// prints X of Y, where X is current page and Y is number of pages
echo $this->Paginator->counter();
The wording output by the counter() method can also be customized using special markers:
echo $this->Paginator->counter(array(
'format' => 'Page {:page} of {:pages}, showing {:current} records out of
{:count} total, starting on record {:start}, ending on {:end}'
));
| Parameters: |
|
|---|
Accepted keys for $options:
Creates a regular or AJAX link with pagination parameters:
echo $this->Paginator->link('Sort by title on page 5',
array('sort' => 'title', 'page' => 5, 'direction' => 'desc'));
If created in the view for /posts/index Would create a link pointing at ‘/posts/index/page:5/sort:title/direction:desc’
| Parameters: |
|
|---|
By default returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript).:
echo $this->Paginator->url(array('sort' => 'title'), true);
Gets the default model of the paged sets or null if pagination is not initialized.
Gets the current paging parameters from the resultset for the given model:
debug($this->Paginator->params());
/*
Array
(
[page] => 2
[current] => 2
[count] => 43
[prevPage] => 1
[nextPage] => 3
[pageCount] => 3
[order] =>
[limit] => 20
[options] => Array
(
[page] => 2
[conditions] => Array
(
)
)
[paramType] => named
)
*/