Skip to content
This repository
Browse code

ENHANCEMENT: Add method to disable automatic limiting on PaginagedLists.

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...
commit 060fca562fe36c9b3aa26f603974cd5a370446ea 1 parent a96659b
Andrew Short authored June 17, 2012
33  core/PaginatedList.php
@@ -13,6 +13,7 @@ class PaginatedList extends SS_ListDecorator {
13 13
 	protected $pageLength = 10;
14 14
 	protected $pageStart;
15 15
 	protected $totalItems;
  16
+	protected $limitItems = true;
16 17
 
17 18
 	/**
18 19
 	 * Constructs a new paginated list instance around a list.
@@ -145,12 +146,38 @@ public function setPaginationFromQuery(SQLQuery $query) {
145 146
 	}
146 147
 
147 148
 	/**
  149
+	 * Returns whether or not the underlying list is limited to the current
  150
+	 * pagination range when iterating.
  151
+	 *
  152
+	 * By default the limit method will be called on the underlying list to
  153
+	 * extract the subset for the current page. In some situations, if the list
  154
+	 * is custom generated and already paginated you don't want to additionally
  155
+	 * limit the list. You can use {@link setLimitItems} to control this.
  156
+	 *
  157
+	 * @return bool
  158
+	 */
  159
+	public function getLimitItems() {
  160
+		return $this->limitItems;
  161
+	}
  162
+
  163
+	/**
  164
+	 * @param bool $limit
  165
+	 */
  166
+	public function setLimitItems($limit) {
  167
+		$this->limitItems = (bool) $limit;
  168
+	}
  169
+
  170
+	/**
148 171
 	 * @return IteratorIterator
149 172
 	 */
150 173
 	public function getIterator() {
151  
-		return new IteratorIterator(
152  
-			$this->list->limit($this->pageLength, $this->getPageStart())
153  
-		);
  174
+		if($this->limitItems) {
  175
+			return new IteratorIterator(
  176
+				$this->list->limit($this->pageLength, $this->getPageStart())
  177
+			);
  178
+		} else {
  179
+			return new IteratorIterator($this->list);
  180
+		}
154 181
 	}
155 182
 
156 183
 	/**
14  docs/en/howto/pagination.md
Source Rendered
@@ -6,7 +6,7 @@ care of fetching a sub-set of the total list and presenting it to the template.
6 6
 
7 7
 In order to create a paginated list, you can create a method on your controller
8 8
 that first creates a `DataList` that will return all pages, and then wraps it
9  
-in a `[api:PaginatedSet]` object. The `PaginatedList` object is also passed the
  9
+in a `[api:PaginatedList]` object. The `PaginatedList` object is also passed the
10 10
 HTTP request object so it can read the current page information from the
11 11
 "?start=" GET var.
12 12
 
@@ -26,7 +26,7 @@ information.
26 26
 
27 27
 Now all that remains is to render this list into a template, along with pagination
28 28
 controls. There are two ways to generate pagination controls:
29  
-`[api:PaginatedSet->Pages()]` and `[api:PaginatedSet->PaginationSummary()]`. In
  29
+`[api:PaginatedList->Pages()]` and `[api:PaginatedList->PaginationSummary()]`. In
30 30
 this example we will use `PaginationSummary()`.
31 31
 
32 32
 The first step is to simply list the objects in the template:
@@ -63,4 +63,12 @@ controls below this so the user can switch between pages:
63 63
 	<% end_if %>
64 64
 
65 65
 If there is more than one page, this block will render a set of pagination
66  
-controls in the form `[1] ... [3] [4] [[5]] [6] [7] ... [10]`.
  66
+controls in the form `[1] ... [3] [4] [[5]] [6] [7] ... [10]`.
  67
+
  68
+## Paginating Custom Lists
  69
+
  70
+In some situations where you are generating the list yourself, the underlying
  71
+list will already contain only the items that you wish to display on the current
  72
+page. In this situation the automatic limiting done by `[api:PaginatedList]`
  73
+will break the pagination. You can disable automatic limiting using the
  74
+`[api:PaginatedList->setLimitItems()]` method when using custom lists.
11  tests/model/PaginatedListTest.php
@@ -135,6 +135,17 @@ public function testPaginationSummary() {
135 135
 		$this->assertDOSEquals($expect, $list->PaginationSummary(4));
136 136
 	}
137 137
 
  138
+	public function testLimitItems() {
  139
+		$list = new ArrayList(range(1, 50));
  140
+		$list = new PaginatedList($list);
  141
+
  142
+		$list->setCurrentPage(3);
  143
+		$this->assertEquals(10, count($list->getIterator()->getInnerIterator()));
  144
+
  145
+		$list->setLimitItems(false);
  146
+		$this->assertEquals(50, count($list->getIterator()->getInnerIterator()));
  147
+	}
  148
+
138 149
 	public function testCurrentPage() {
139 150
 		$list = new PaginatedList(new ArrayList());
140 151
 		$list->setTotalItems(50);

0 notes on commit 060fca5

Please sign in to comment.
Something went wrong with that request. Please try again.