Documentation

Pagination Control and ScrollingStyles - Getting Started with Zend_Paginator

Pagination Control and ScrollingStyles

Rendering the items for a page on the screen has been a good start. In the code snippets in previous section we have also seen the setCurrentPageNumber() method to set the active page number. The next step is to navigate through your pages. To do this, Paginator provides you with two important tools: the ability to render the Paginator with help of a View Partial, and support for so-called ScrollingStyles.

The View Partial is a small view script that renders the Pagination controls, such as buttons to go to the next or previous page. Which pagination controls are rendered depends on the contents of the view partial. Working with the view partial requires that you have set up Zend_View. To get started with the pagination control, create a new view script somewhere in your view scripts path. You can name it anything you want, but we'll call it "controls.phtml" in this text. The reference manual contains various examples of what might go in the view script. Here is one example.

  1. <?php if ($this->pageCount): ?>
  2. <!-- First page link -->
  3. <?php if (isset($this->previous)): ?>
  4.   <a href="<?php echo $this->url(array('page' => $this->first)); ?>">
  5.     First
  6.   </a> |
  7. <?php else: ?>
  8.   <span class="disabled">First</span> |
  9. <?php endif; ?>
  10.  
  11. <!-- Previous page link -->
  12. <?php if (isset($this->previous)): ?>
  13.   <a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
  14.     &lt; Previous
  15.   </a> |
  16. <?php else: ?>
  17.   <span class="disabled">&lt; Previous</span> |
  18. <?php endif; ?>
  19.  
  20. <!-- Next page link -->
  21. <?php if (isset($this->next)): ?>
  22.   <a href="<?php echo $this->url(array('page' => $this->next)); ?>">
  23.     Next &gt;
  24.   </a> |
  25. <?php else: ?>
  26.   <span class="disabled">Next &gt;</span> |
  27. <?php endif; ?>
  28.  
  29. <!-- Last page link -->
  30. <?php if (isset($this->next)): ?>
  31.   <a href="<?php echo $this->url(array('page' => $this->last)); ?>">
  32.     Last
  33.   </a>
  34. <?php else: ?>
  35.   <span class="disabled">Last</span>
  36. <?php endif; ?>
  37.  
  38. </div>
  39. <?php endif; ?>

The next step is to tell Zend_Paginator which view partial can be used to render the navigation controls. Put the following line in your application's bootstrap file.

  1. Zend_View_Helper_PaginationControl::setDefaultViewPartial('controls.phtml');

The last step is probably the easiest. Make sure you have assigned your Paginator object to the a script (NOT the 'controls.phtml' script!). The only thing left to do is echo the Paginator in the view script. This will automatically render the Paginator using the PaginationControl view helper. In this next example, the Paginator object has been assigned to the 'paginator' view variable. Don't worry if you don't fully get how it all works yet. The next section will feature a complete example.

  1. <?php echo $this->paginator; ?>

Zend_Paginator, together with the 'controls.phtml' view script you wrote, makes sure your Paginator navigation is rendered properly. In order to decide which page numbers need to be displayed on screen, Paginator uses so-called ScrollingStyles. The default style is called "Sliding", which is similar to the way Yahoo's search result navigation works. To mimick Google's ScrollingStyle, use the Elastic style. You can set a default ScrollingStyle with the static setDefaultScrollingStyle() method, or you can specify a ScrollingStyle dynamically when rendering the Paginator in your view script. This requires manual invocation of the view helper in your view script.

  1. // $this->paginator is a Paginator object
  2. <?php echo $this->paginationControl($this->paginator, 'Elastic', 'controls.phtml'); ?>

For a list of all available ScrollingStyles, see the reference manual.

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts