Skip to content

Commit

Permalink
ENHANCEMENT: Add method to disable automatic limiting on PaginagedLists.
Browse files Browse the repository at this point in the history
In some circumstances a custom generated list will already only contain
the items for the current page. The automatic limiting will then limit
the already limited list, breaking pagination. This allows you to disable
automatic limiting so all items are shown regardless of the current page.
  • Loading branch information
ajshort committed Jun 17, 2012
1 parent a96659b commit 060fca5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
33 changes: 30 additions & 3 deletions core/PaginatedList.php
Expand Up @@ -13,6 +13,7 @@ class PaginatedList extends SS_ListDecorator {
protected $pageLength = 10;
protected $pageStart;
protected $totalItems;
protected $limitItems = true;

/**
* Constructs a new paginated list instance around a list.
Expand Down Expand Up @@ -144,13 +145,39 @@ public function setPaginationFromQuery(SQLQuery $query) {
}
}

/**
* Returns whether or not the underlying list is limited to the current
* pagination range when iterating.
*
* By default the limit method will be called on the underlying list to
* extract the subset for the current page. In some situations, if the list
* is custom generated and already paginated you don't want to additionally
* limit the list. You can use {@link setLimitItems} to control this.
*
* @return bool
*/
public function getLimitItems() {
return $this->limitItems;
}

/**
* @param bool $limit
*/
public function setLimitItems($limit) {
$this->limitItems = (bool) $limit;
}

/**
* @return IteratorIterator
*/
public function getIterator() {
return new IteratorIterator(
$this->list->limit($this->pageLength, $this->getPageStart())
);
if($this->limitItems) {
return new IteratorIterator(
$this->list->limit($this->pageLength, $this->getPageStart())
);
} else {
return new IteratorIterator($this->list);
}
}

/**
Expand Down
14 changes: 11 additions & 3 deletions docs/en/howto/pagination.md
Expand Up @@ -6,7 +6,7 @@ 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
that first creates a `DataList` that will return all pages, and then wraps it
in a `[api:PaginatedSet]` object. The `PaginatedList` object is also passed the
in a `[api:PaginatedList]` object. The `PaginatedList` object is also passed the
HTTP request object so it can read the current page information from the
"?start=" GET var.

Expand All @@ -26,7 +26,7 @@ information.

Now all that remains is to render this list into a template, along with pagination
controls. There are two ways to generate pagination controls:
`[api:PaginatedSet->Pages()]` and `[api:PaginatedSet->PaginationSummary()]`. In
`[api:PaginatedList->Pages()]` and `[api:PaginatedList->PaginationSummary()]`. In
this example we will use `PaginationSummary()`.

The first step is to simply list the objects in the template:
Expand Down Expand Up @@ -63,4 +63,12 @@ controls below this so the user can switch between pages:
<% end_if %>

If there is more than one page, this block will render a set of pagination
controls in the form `[1] ... [3] [4] [[5]] [6] [7] ... [10]`.
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.
11 changes: 11 additions & 0 deletions tests/model/PaginatedListTest.php
Expand Up @@ -135,6 +135,17 @@ public function testPaginationSummary() {
$this->assertDOSEquals($expect, $list->PaginationSummary(4));
}

public function testLimitItems() {
$list = new ArrayList(range(1, 50));
$list = new PaginatedList($list);

$list->setCurrentPage(3);
$this->assertEquals(10, count($list->getIterator()->getInnerIterator()));

$list->setLimitItems(false);
$this->assertEquals(50, count($list->getIterator()->getInnerIterator()));
}

public function testCurrentPage() {
$list = new PaginatedList(new ArrayList());
$list->setTotalItems(50);
Expand Down

0 comments on commit 060fca5

Please sign in to comment.