2011-03-31 03:01:04 +02:00
|
|
|
# Paginating A List
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
Adding pagination to a `[api:SS_List]` is quite simple. All
|
2011-03-31 03:01:04 +02:00
|
|
|
you need to do is wrap the object in a `[api:PaginatedList]` decorator, which takes
|
|
|
|
care of fetching a sub-set of the total list and presenting it to the template.
|
|
|
|
|
|
|
|
In order to create a paginated list, you can create a method on your controller
|
2012-06-23 00:32:43 +02:00
|
|
|
that first creates a `SS_List` that will return all pages, and then wraps it
|
2012-06-17 06:01:48 +02:00
|
|
|
in a `[api:PaginatedList]` object. The `PaginatedList` object is also passed the
|
2011-03-31 03:01:04 +02:00
|
|
|
HTTP request object so it can read the current page information from the
|
|
|
|
"?start=" GET var.
|
|
|
|
|
|
|
|
The paginator will automatically set up query limits and read the request for
|
|
|
|
information.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
/**
|
|
|
|
* Returns a paginated list of all pages in the site.
|
|
|
|
*/
|
|
|
|
public function PaginatedPages() {
|
2012-06-23 00:32:43 +02:00
|
|
|
return new PaginatedList(Page::get(), $this->request);
|
2011-03-31 03:01:04 +02:00
|
|
|
}
|
|
|
|
|
2012-06-27 16:09:21 +02:00
|
|
|
Note that the concept of "pages" used in pagination does not necessarily
|
|
|
|
mean that we're dealing with `Page` classes, its just a term to describe
|
|
|
|
a sub-collection of the list.
|
|
|
|
|
2011-03-31 03:01:04 +02:00
|
|
|
## Setting Up The Template
|
|
|
|
|
|
|
|
Now all that remains is to render this list into a template, along with pagination
|
|
|
|
controls. There are two ways to generate pagination controls:
|
2012-06-17 06:01:48 +02:00
|
|
|
`[api:PaginatedList->Pages()]` and `[api:PaginatedList->PaginationSummary()]`. In
|
2011-03-31 03:01:04 +02:00
|
|
|
this example we will use `PaginationSummary()`.
|
|
|
|
|
|
|
|
The first step is to simply list the objects in the template:
|
|
|
|
|
|
|
|
:::ss
|
|
|
|
<ul>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $PaginatedPages %>
|
2011-03-31 03:01:04 +02:00
|
|
|
<li><a href="$Link">$Title</a></li>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2011-03-31 03:01:04 +02:00
|
|
|
</ul>
|
|
|
|
|
|
|
|
By default this will display 10 pages at a time. The next step is to add pagination
|
|
|
|
controls below this so the user can switch between pages:
|
|
|
|
|
|
|
|
:::ss
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $PaginatedPages.MoreThanOnePage %>
|
|
|
|
<% if $PaginatedPages.NotFirstPage %>
|
2011-03-31 03:01:04 +02:00
|
|
|
<a class="prev" href="$PaginatedPages.PrevLink">Prev</a>
|
|
|
|
<% end_if %>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% loop $PaginatedPages.Pages %>
|
|
|
|
<% if $CurrentBool %>
|
2011-03-31 03:01:04 +02:00
|
|
|
$PageNum
|
|
|
|
<% else %>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $Link %>
|
2011-03-31 03:01:04 +02:00
|
|
|
<a href="$Link">$PageNum</a>
|
|
|
|
<% else %>
|
|
|
|
...
|
|
|
|
<% end_if %>
|
|
|
|
<% end_if %>
|
2012-06-26 17:32:46 +02:00
|
|
|
<% end_loop %>
|
2013-04-26 11:48:59 +02:00
|
|
|
<% if $PaginatedPages.NotLastPage %>
|
2011-03-31 03:01:04 +02:00
|
|
|
<a class="next" href="$PaginatedPages.NextLink">Next</a>
|
|
|
|
<% end_if %>
|
|
|
|
<% end_if %>
|
|
|
|
|
|
|
|
If there is more than one page, this block will render a set of pagination
|
2012-06-17 06:01:48 +02:00
|
|
|
controls in the form `[1] ... [3] [4] [[5]] [6] [7] ... [10]`.
|
|
|
|
|
|
|
|
## Paginating Custom Lists
|
|
|
|
|
|
|
|
In some situations where you are generating the list yourself, the underlying
|
|
|
|
list will already contain only the items that you wish to display on the current
|
|
|
|
page. In this situation the automatic limiting done by `[api:PaginatedList]`
|
|
|
|
will break the pagination. You can disable automatic limiting using the
|
|
|
|
`[api:PaginatedList->setLimitItems()]` method when using custom lists.
|
2012-06-23 00:32:43 +02:00
|
|
|
|
2014-01-30 23:20:17 +01:00
|
|
|
## Setting the limit of items to be displayed on a page ##
|
|
|
|
|
|
|
|
To set the limit of items displayed in a paginated page use the `[api:PaginatedList->setPageLength()]` method. e.g:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
/**
|
|
|
|
* Returns a paginated list of all pages in the site, and limits the items displayed to 4 per page.
|
|
|
|
*/
|
|
|
|
public function PaginatedPagesLimit() {
|
|
|
|
$paginatedItems = new PaginatedList(Page::get(), $this->request);
|
|
|
|
$paginatedItems->setPageLength(4);
|
2014-05-08 22:46:30 +02:00
|
|
|
return $paginatedItems;
|
2014-01-30 23:20:17 +01:00
|
|
|
}
|
|
|
|
|
2012-06-23 00:32:43 +02:00
|
|
|
## Related
|
|
|
|
|
2014-01-30 23:20:17 +01:00
|
|
|
* [Howto: "Grouping Lists"](/howto/grouping-dataobjectsets)
|