发布于 2015-10-07 10:01:37 | 146 次阅读 | 评论: 0 | 来源: 网络整理

30.4. Advanced usage

30.4.1. Custom data source adapters

At some point you may run across a data type that is not covered by the packaged adapters. In this case, you will need to write your own.

To do so, you must implement Zend_Paginator_Adapter_Interface. There are two methods required to do this:

  • count()

  • getItems($offset, $itemCountPerPage)

Additionally, you'll want to implement a constructor that takes your data source as a parameter and stores it as a protected or private property. How you wish to go about doing this specifically is up to you.

If you've ever used the SPL interface Countable, you're familiar with count(). As used with Zend_Paginator, this is the total number of items in the data collection.

The getItems() method is only slightly more complicated. For this, your adapter is supplied with an offset and the number of items to display per page. You must return the appropriate slice of data. For an array, that would be:

return array_slice($this->_array, $offset, $itemCountPerPage);

            

Take a look at the packaged adapters (all of which implement the Zend_Paginator_Adapter_Interface) for ideas of how you might go about implementing your own.

30.4.2. Custom scrolling styles

Creating your own scrolling style requires that you implement Zend_Paginator_ScrollingStyle_Interface, which defines a single method, getPages(). Specifically,

public function getPages(Zend_Paginator $paginator, $pageRange = null);

            

This method should calculate a lower and upper bound for page numbers within the range of so-called "local" pages (that is, pages that are nearby the current page).

Unless it extends another scrolling style (see Zend_Paginator_ScrollingStyle_Elastic for an example), your custom scrolling style will inevitably end with something similar to the following line of code:

return $paginator->getPagesInRange($lowerBound, $upperBound);

            

There's nothing special about this call; it's merely a convenience method to check the validity of the lower and upper bound and return an array of the range to the paginator.

When you're ready to use your new scrolling style, you'll need to tell Zend_Paginator what directory to look in. To do that, do the following:

$prefix = 'My_Paginator_ScrollingStyle';
$path   = 'My/Paginator/ScrollingStyle/';
Zend_Paginator::addScrollingStylePrefixPath($prefix, $path);

            

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务