Zend_Paginator-Usage.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.paginator.usage">
  4. <title>Usage</title>
  5. <sect2 id="zend.paginator.usage.paginating">
  6. <title>Paginating data collections</title>
  7. <para>
  8. In order to paginate items into pages, <classname>Zend_Paginator</classname>
  9. must have a generic way of accessing that data. For that reason,
  10. all data access takes place through data source adapters. Several
  11. adapters ship with Zend Framework by default:
  12. </para>
  13. <table id="zend.paginator.usage.paginating.adapters">
  14. <title>Adapters for Zend_Paginator</title>
  15. <tgroup cols="2">
  16. <thead>
  17. <row>
  18. <entry>Adapter</entry>
  19. <entry>Description</entry>
  20. </row>
  21. </thead>
  22. <tbody>
  23. <row>
  24. <entry>Array</entry>
  25. <entry>Use a PHP array</entry>
  26. </row>
  27. <row>
  28. <entry>DbSelect</entry>
  29. <entry>
  30. Use a <link linkend="zend.db.select"><classname>Zend_Db_Select</classname></link>
  31. instance, which will return an array
  32. </entry>
  33. </row>
  34. <row>
  35. <entry>DbTableSelect</entry>
  36. <entry>
  37. Use a <link linkend="zend.db.table.fetch-all"><classname>Zend_Db_Table_Select</classname></link>
  38. instance, which will return an instance of
  39. <classname>Zend_Db_Table_Rowset_Abstract</classname>.
  40. This provides additional information about the
  41. result set, such as column names.
  42. </entry>
  43. </row>
  44. <row>
  45. <entry>Iterator</entry>
  46. <entry>
  47. Use an <ulink url="http://www.php.net/~helly/php/ext/spl/interfaceIterator.html"><code>Iterator</code></ulink>
  48. instance
  49. </entry>
  50. </row>
  51. <row>
  52. <entry>Null</entry>
  53. <entry>
  54. Do not use <classname>Zend_Paginator</classname> to manage
  55. data pagination. You can still take advantage of
  56. the pagination control feature.
  57. </entry>
  58. </row>
  59. </tbody>
  60. </tgroup>
  61. </table>
  62. <note>
  63. <para>
  64. Instead of selecting every matching row of a given query, the
  65. DbSelect and DbTableSelect adapters retrieve only the smallest
  66. amount of data necessary for displaying the current page.
  67. </para>
  68. <para>
  69. Because of this, a second query is dynamically generated to
  70. determine the total number of matching rows. However, it is
  71. possible to directly supply a count or count query yourself.
  72. See the <code>setRowCount()</code> method in the DbSelect
  73. adapter for more information.
  74. </para>
  75. </note>
  76. <para>
  77. To create an instance of <classname>Zend_Paginator</classname>, you must
  78. supply an adapter to the constructor:
  79. </para>
  80. <para>
  81. <programlisting language="php"><![CDATA[
  82. $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($array));
  83. ]]></programlisting>
  84. </para>
  85. <para>
  86. For convenience, you may take advantage of the static
  87. <code>factory()</code> method for the adapters packaged with Zend
  88. Framework:
  89. </para>
  90. <para>
  91. <programlisting language="php"><![CDATA[
  92. $paginator = Zend_Paginator::factory($array);
  93. ]]></programlisting>
  94. </para>
  95. <note>
  96. <para>
  97. In the case of the Null adapter, in lieu of a data collection
  98. you must supply an item count to its constructor.
  99. </para>
  100. </note>
  101. <para>
  102. Although the instance is technically usable in this state, in your
  103. controller action you'll need to tell the paginator what page
  104. number the user requested. This allows him to advance through the
  105. paginated data.
  106. </para>
  107. <para>
  108. <programlisting language="php"><![CDATA[
  109. $paginator->setCurrentPageNumber($page);
  110. ]]></programlisting>
  111. </para>
  112. <para>
  113. The simplest way to keep track of this value is through a URL.
  114. Although we recommend using a
  115. <classname>Zend_Controller_Router_Interface</classname>-compatible
  116. router to handle this, it is not a requirement.
  117. </para>
  118. <para>
  119. The following is an example route you might use in an INI
  120. configuration file:
  121. </para>
  122. <para>
  123. <programlisting language="php"><![CDATA[
  124. routes.example.route = articles/:articleName/:page
  125. routes.example.defaults.controller = articles
  126. routes.example.defaults.action = view
  127. routes.example.defaults.page = 1
  128. routes.example.reqs.articleName = \w+
  129. routes.example.reqs.page = \d+
  130. ]]></programlisting>
  131. </para>
  132. <para>
  133. With the above route (and using Zend Framework MVC components),
  134. you might set the current page number like this:
  135. </para>
  136. <para>
  137. <programlisting language="php"><![CDATA[
  138. $paginator->setCurrentPageNumber($this->_getParam('page'));
  139. ]]></programlisting>
  140. </para>
  141. <para>
  142. There are other options available; see
  143. <link linkend="zend.paginator.configuration">Configuration</link>
  144. for more on them.
  145. </para>
  146. <para>
  147. Finally, you'll need to assign the paginator instance to your view.
  148. If you're using <classname>Zend_View</classname> with the ViewRenderer action
  149. helper, the following will work:
  150. </para>
  151. <para>
  152. <programlisting language="php"><![CDATA[
  153. $this->view->paginator = $paginator;
  154. ]]></programlisting>
  155. </para>
  156. </sect2>
  157. <sect2 id="zend.paginator.rendering">
  158. <title>Rendering pages with view scripts</title>
  159. <para>
  160. The view script is used to render the page items (if you're using
  161. <classname>Zend_Paginator</classname> to do so) and display the pagination
  162. control.
  163. </para>
  164. <para>
  165. Because <classname>Zend_Paginator</classname> implements the SPL interface
  166. <ulink url="http://www.php.net/~helly/php/ext/spl/interfaceIteratorAggregate.html"><code>IteratorAggregate</code></ulink>,
  167. looping over your items and displaying them is simple.
  168. </para>
  169. <para>
  170. <programlisting language="php"><![CDATA[
  171. <html>
  172. <body>
  173. <h1>Example</h1>
  174. <?php if (count($this->paginator)): ?>
  175. <ul>
  176. <?php foreach ($this->paginator as $item): ?>
  177. <li><?php echo $item; ?></li>
  178. <?php endforeach; ?>
  179. </ul>
  180. <?php endif; ?>
  181. <?php echo $this->paginationControl($this->paginator,
  182. 'Sliding',
  183. 'my_pagination_control.phtml'); ?>
  184. </body>
  185. </html>
  186. ]]></programlisting>
  187. </para>
  188. <para>
  189. Notice the view helper call near the end. PaginationControl
  190. accepts up to four parameters: the paginator instance, a scrolling
  191. style, a view partial, and an array of additional parameters.
  192. </para>
  193. <para>
  194. The second and third parameters are very important. Whereas the
  195. view partial is used to determine how the pagination control should
  196. <emphasis>look</emphasis>, the scrolling style is used to control
  197. how it should <emphasis>behave</emphasis>. Say the view partial is
  198. in the style of a search pagination control, like the one below:
  199. </para>
  200. <para>
  201. <inlinegraphic align="center" valign="middle"
  202. fileref="figures/zend.paginator.usage.rendering.control.png"
  203. format="PNG"/>
  204. </para>
  205. <para>
  206. What happens when the user clicks the "next" link a few times?
  207. Well, any number of things could happen. The current page number
  208. could stay in the middle as you click through (as it does on
  209. Yahoo!), or it could advance to the end of the page range and then
  210. appear again on the left when the user clicks "next" one more time.
  211. The page numbers might even expand and contract as the user
  212. advances (or "scrolls") through them (as they do on Google).
  213. </para>
  214. <para>
  215. There are four scrolling styles packaged with Zend Framework:
  216. </para>
  217. <table id="zend.paginator.usage.rendering.scrolling-styles">
  218. <title>Scrolling styles for Zend_Paginator</title>
  219. <tgroup cols="2">
  220. <thead>
  221. <row>
  222. <entry>Scrolling style</entry>
  223. <entry>Description</entry>
  224. </row>
  225. </thead>
  226. <tbody>
  227. <row>
  228. <entry>All</entry>
  229. <entry>
  230. Returns every page. This is useful for dropdown
  231. menu pagination controls with relatively few
  232. pages. In these cases, you want all pages
  233. available to the user at once.
  234. </entry>
  235. </row>
  236. <row>
  237. <entry>Elastic</entry>
  238. <entry>
  239. A Google-like scrolling style that expands and
  240. contracts as a user scrolls through the pages.
  241. </entry>
  242. </row>
  243. <row>
  244. <entry>Jumping</entry>
  245. <entry>
  246. As users scroll through, the page number advances
  247. to the end of a given range, then starts again at
  248. the beginning of the new range.
  249. </entry>
  250. </row>
  251. <row>
  252. <entry>Sliding</entry>
  253. <entry>
  254. A Yahoo!-like scrolling style that positions the
  255. current page number in the center of the page
  256. range, or as close as possible. This is the
  257. default style.
  258. </entry>
  259. </row>
  260. </tbody>
  261. </tgroup>
  262. </table>
  263. <para>
  264. The fourth and final parameter is reserved for an optional
  265. associative array of additional variables that you want available
  266. in your view partial (available via <code>$this</code>). For
  267. instance, these values could include extra URL parameters for
  268. pagination links.
  269. </para>
  270. <para>
  271. By setting the default view partial, default
  272. scrolling style, and view instance, you can eliminate the calls to
  273. PaginationControl completely:
  274. </para>
  275. <para>
  276. <programlisting language="php"><![CDATA[
  277. Zend_Paginator::setDefaultScrollingStyle('Sliding');
  278. Zend_View_Helper_PaginationControl::setDefaultViewPartial(
  279. 'my_pagination_control.phtml'
  280. );
  281. $paginator->setView($view);
  282. ]]></programlisting>
  283. </para>
  284. <para>
  285. When all of these values are set, you can render the pagination
  286. control inside your view script with a simple echo statement:
  287. </para>
  288. <para>
  289. <programlisting language="php"><![CDATA[
  290. <?php echo $this->paginator; ?>
  291. ]]></programlisting>
  292. </para>
  293. <note>
  294. <para>
  295. Of course, it's possible to use <classname>Zend_Paginator</classname>
  296. with other template engines. For example, with Smarty you
  297. might do the following:
  298. </para>
  299. <para>
  300. <programlisting language="php"><![CDATA[
  301. $smarty->assign('pages', $paginator->getPages());
  302. ]]></programlisting>
  303. </para>
  304. <para>
  305. You could then access paginator values from a template like so:
  306. </para>
  307. <para>
  308. <programlisting language="php"><![CDATA[
  309. {$pages.pageCount}
  310. ]]></programlisting>
  311. </para>
  312. </note>
  313. <sect3 id="zend.paginator.usage.rendering.example-controls">
  314. <title>Example pagination controls</title>
  315. <para>
  316. The following example pagination controls will hopefully help
  317. you get started:
  318. </para>
  319. <para>
  320. Search pagination
  321. <programlisting language="php"><![CDATA[
  322. <!--
  323. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=searchpagination
  324. -->
  325. <?php if ($this->pageCount): ?>
  326. <div class="paginationControl">
  327. <!-- Previous page link -->
  328. <?php if (isset($this->previous)): ?>
  329. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  330. &lt; Previous
  331. </a> |
  332. <?php else: ?>
  333. <span class="disabled">&lt; Previous</span> |
  334. <?php endif; ?>
  335. <!-- Numbered page links -->
  336. <?php foreach ($this->pagesInRange as $page): ?>
  337. <?php if ($page != $this->current): ?>
  338. <a href="<?php echo $this->url(array('page' => $page)); ?>">
  339. <?php echo $page; ?>
  340. </a> |
  341. <?php else: ?>
  342. <?php echo $page; ?> |
  343. <?php endif; ?>
  344. <?php endforeach; ?>
  345. <!-- Next page link -->
  346. <?php if (isset($this->next)): ?>
  347. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  348. Next &gt;
  349. </a>
  350. <?php else: ?>
  351. <span class="disabled">Next &gt;</span>
  352. <?php endif; ?>
  353. </div>
  354. <?php endif; ?>
  355. ]]></programlisting>
  356. </para>
  357. <para>
  358. Item pagination
  359. <programlisting language="php"><![CDATA[
  360. <!--
  361. See http://developer.yahoo.com/ypatterns/pattern.php?pattern=itempagination
  362. -->
  363. <?php if ($this->pageCount): ?>
  364. <div class="paginationControl">
  365. <?php echo $this->firstItemNumber; ?> - <?php echo $this->lastItemNumber; ?>
  366. of <?php echo $this->totalItemCount; ?>
  367. <!-- First page link -->
  368. <?php if (isset($this->previous)): ?>
  369. <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
  370. First
  371. </a> |
  372. <?php else: ?>
  373. <span class="disabled">First</span> |
  374. <?php endif; ?>
  375. <!-- Previous page link -->
  376. <?php if (isset($this->previous)): ?>
  377. <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  378. &lt; Previous
  379. </a> |
  380. <?php else: ?>
  381. <span class="disabled">&lt; Previous</span> |
  382. <?php endif; ?>
  383. <!-- Next page link -->
  384. <?php if (isset($this->next)): ?>
  385. <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  386. Next &gt;
  387. </a> |
  388. <?php else: ?>
  389. <span class="disabled">Next &gt;</span> |
  390. <?php endif; ?>
  391. <!-- Last page link -->
  392. <?php if (isset($this->next)): ?>
  393. <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
  394. Last
  395. </a>
  396. <?php else: ?>
  397. <span class="disabled">Last</span>
  398. <?php endif; ?>
  399. </div>
  400. <?php endif; ?>
  401. ]]></programlisting>
  402. </para>
  403. <para>
  404. Dropdown pagination
  405. <programlisting language="php"><![CDATA[
  406. <?php if ($this->pageCount): ?>
  407. <select id="paginationControl" size="1">
  408. <?php foreach ($this->pagesInRange as $page): ?>
  409. <?php $selected = ($page == $this->current) ? ' selected="selected"' : ''; ?>
  410. <option value="<?php
  411. echo $this->url(array('page' => $page));?>"<?php echo $selected ?>>
  412. <?php echo $page; ?>
  413. </option>
  414. <?php endforeach; ?>
  415. </select>
  416. <?php endif; ?>
  417. <script type="text/javascript"
  418. src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js">
  419. </script>
  420. <script type="text/javascript">
  421. $('paginationControl').observe('change', function() {
  422. window.location = this.options[this.selectedIndex].value;
  423. })
  424. </script>
  425. ]]></programlisting>
  426. </para>
  427. </sect3>
  428. <sect3 id="zend.paginator.usage.rendering.properties">
  429. <title>Listing of properties</title>
  430. <para>
  431. The following options are available to pagination control
  432. view partials:
  433. </para>
  434. <table id="zend.paginator.usage.rendering.properties.table">
  435. <title>Properties available to view partials</title>
  436. <tgroup cols="3">
  437. <thead>
  438. <row>
  439. <entry>Property</entry>
  440. <entry>Type</entry>
  441. <entry>Description</entry>
  442. </row>
  443. </thead>
  444. <tbody>
  445. <row>
  446. <entry>first</entry>
  447. <entry>integer</entry>
  448. <entry>First page number (i.e., 1)</entry>
  449. </row>
  450. <row>
  451. <entry>firstItemNumber</entry>
  452. <entry>integer</entry>
  453. <entry>
  454. Absolute number of the first item on this page
  455. </entry>
  456. </row>
  457. <row>
  458. <entry>firstPageInRange</entry>
  459. <entry>integer</entry>
  460. <entry>
  461. First page in the range returned by the
  462. scrolling style
  463. </entry>
  464. </row>
  465. <row>
  466. <entry>current</entry>
  467. <entry>integer</entry>
  468. <entry>Current page number</entry>
  469. </row>
  470. <row>
  471. <entry>currentItemCount</entry>
  472. <entry>integer</entry>
  473. <entry>Number of items on this page</entry>
  474. </row>
  475. <row>
  476. <entry>itemCountPerPage</entry>
  477. <entry>integer</entry>
  478. <entry>Maximum number of items available to each page</entry>
  479. </row>
  480. <row>
  481. <entry>last</entry>
  482. <entry>integer</entry>
  483. <entry>Last page number</entry>
  484. </row>
  485. <row>
  486. <entry>lastItemNumber</entry>
  487. <entry>integer</entry>
  488. <entry>
  489. Absolute number of the last item on this page
  490. </entry>
  491. </row>
  492. <row>
  493. <entry>lastPageInRange</entry>
  494. <entry>integer</entry>
  495. <entry>
  496. Last page in the range returned by the
  497. scrolling style
  498. </entry>
  499. </row>
  500. <row>
  501. <entry>next</entry>
  502. <entry>integer</entry>
  503. <entry>Next page number</entry>
  504. </row>
  505. <row>
  506. <entry>pageCount</entry>
  507. <entry>integer</entry>
  508. <entry>Number of pages</entry>
  509. </row>
  510. <row>
  511. <entry>pagesInRange</entry>
  512. <entry>array</entry>
  513. <entry>
  514. Array of pages returned by the scrolling style
  515. </entry>
  516. </row>
  517. <row>
  518. <entry>previous</entry>
  519. <entry>integer</entry>
  520. <entry>Previous page number</entry>
  521. </row>
  522. <row>
  523. <entry>totalItemCount</entry>
  524. <entry>integer</entry>
  525. <entry>Total number of items</entry>
  526. </row>
  527. </tbody>
  528. </tgroup>
  529. </table>
  530. </sect3>
  531. </sect2>
  532. </sect1>
  533. <!--
  534. vim:se ts=4 sw=4 et:
  535. -->