Skip to content

Dev.Module Pagination

Taiwen Jiang edited this page Mar 14, 2013 · 1 revision

Pi Pagination Guide

Contents

  • Methods in paginator class
  • The example of paginator

Methods in paginator class

Pi provides us a package of pagination, by using the method, users can realize a pagination function conveniently.

Here are the methods in paginator class:

paginator factory(mixed $data);
setGlobalConfig(array|\Traversable $config);
setOptions(array $config);
AdapterInterface createAdapter(string $adapter, mixed $data);
ScrollingStyleInterface createScrollingStyle(string $style);
string getDefaultScrollingStyle();
setDefaultScrollingStyle(string $scrollingStyle = 'Sliding');
int getDefaultItemCountPerPage();
setDefaultItemCountPerPage(int $count);
int getItemCountPerPage();
Paginator setItemCountPerPage(int $itemCountPerPage = -1);
int count();
int getTotalItemCount();
int getAbsoluteItemNumber(int $relativeItemNumber, int $pageNumber = null);
AdapterInterface getAdapter();
int getCurrentItemCount();
Traversable getCurrentItems();
int getCurrentPageNumber();
Paginator setCurrentPageNumber(int $pageNumber);
FilterInterface getFilter();
Paginator setFilter(FilterInterface $filter);
mixed getItem(int $itemNumber, int $pageNumber = null);
int getItemCount(mixed $items);
mixed getItemsByPage($pageNumber);
int getPageRange();
Paginator setPageRange($pageRange);
array getPages(string $scrollingStyle = null);
array getPagesInRange(int $lowerBound, int $upperBound)
Zend\View\Renderer\RendererInterface getView();
Paginator setView(Zend\View\Renderer\RendererInterface $view = null);
string render(Zend\View\Renderer\RendererInterface $view = null);
string toJson();
Paginator setUrlOptions(array $options);
string buildUrl(int $page);

factory()

This method is used to create a Paginator handler, the parameter can be an array, an instance of DbTableSelect, DbSelect or Iterator. Then user can use this handler to set parameters and configure paginator.

For example:

$select = $model->select()->where()->order('id');
$rowset = Pi::model('tbname')->selectWith($select)->toArray();
$paginator = \Pi\Paginator\Paginator::factory($pages);

Another usage:

$select = $model->select()->columns(array('count' => new \Zend\Db\Sql\Predicate\Expression('count(*)')));
$count = $model->selectWith($select)->current()->count;       
$paginator = \Pi\Paginator\Paginator::factory(intval($count));

setGlobalConfig() and setOptions()

These two method is used to set global configuration, such as scrolling plugin and scrolling style. The setOptions() method can also configure other parameters except scrolling_style.

$paginator->setGlobalConfig(array(
    'scrolling_style_plugins'   => $adapter,
    'scrolling_style'           => 'Sliding',
));

$paginator->setOptions(array(
    'scrolling_style'    => 'Sliding'
));

createAdapter() and getAdapter()

The createAdapter method is used to create a paginator adapter by passed parameters. The adapter in Pi package has the highest priority.

This method takes two parameters, the first one is the adapter name, and the second parameter is the parameter to construct the adapter class.

The getAdapter() method is used to get the adapter handler.

$paginator->createAdapter('DbSelect', array());
$paginator->createAdapter('Int', array());
$adapter = $paginator->getAdapter();

In the example one, the DbSelect is exist, I take this as example on purpose to show what is a adapter, users do not need to create a DbSelect, but users should have to create a adapter file by themselves if the created adapter is not exists.

**createScrollingStyle()`

This method is used to create a scrolling style handler which is exists in Paginator\\ScrollingStyle package. The program will also first search the class in Pi package, then class in Zend package will be searched if it is not exist in Pi.

$paginator->createScrollingStyle('Sliding');

getDefaultScrollingStyle() and setDefaultScrollingStyle()

These two methods are used to operate the scrolling style, the parameter of setDefaultScrollingStyle() is the name of scrolling style.

$paginator->setDefaultScrollingStyle('Sliding');
echo $paginator->getDefaultScrollingStyle();

This code will output:

'Sliding'

getDefaultItemCountPerPage() and setDefaultItemCountPerPage()

These two methods are used to operate the limitation of each page. The default limitation is only used when users do not set the limitation for paginator.

$paginator->setDefaultItemCountPerPage(10);
echo $paginator->getDefaultItemCountPerPage();

This code will output:

10

getItemCountPerPage() and setItemCountPerPage()

These two methods are used for setting the limitation of paginator, application will use the setting parameter to limit the count per page.

The parameter of setItemCountPerPage() method should be larger than 1, or else the item count will be set equal to total item count.

The default item count will be return if user haven't set the item count when using getItemCountPerPage() method.

$paginator->setItemCountPerPage(20);
echo $paginator->getItemCountPerPage();

Output:

20

count()

This method is used to get the count of pages.

$count = $paginator->count();

getTotalItemCount()

This method is used to get the total number of items available.

$totalCount = $paginator->getTotalItemCount();

getAbsoluteItemNumber()

This method is used to get an item's absolute position (absolute number) in all items, it takes two parameters, the first one is the relative position within the page, and the second parameter is the page number this item belonged to.

For example:

echo $paginator->getAbsoluteItemNumber(12, 7);

If the page count is 20, it will output:

152

getCurrentPageNumber() and setCurrentPageNumber()

This two method is used to operate current page number (page position), this method should be called by user when they want to use paginator.

$page = $this->params('p', 1);
$paginator->setCurrentPageNumber($page);
echo $paginator->getCurrentPageNumber();

Output:

1

getCurrentItems()

This method is used fetch all items of current page. These results will be filter accroding to the given filter if it is set.

$items = $paginator->getCurrentItems();

getFilter() and setFilter()

These two methods is used to operate filter for paginator, the parameter of setFilter() must be a FilterInterface type.

$paginator->setFilter(new \Zend\Filter\Int);

getItemCount()

This method is used to get the count of items in a collection, the parameter type of this method must be array, or instance of Countable and Traversable.

getCurrentItemCount()

This method is used to get the count of items for current page.

echo $paginator->getCurrentItemCount();

If count of current page is 20, it will output:

20

getItem()

This method is used to fetch an item from a page, it takes two parameters.

The first one describes the item number, if this parameter is set to negative, it will fetch the item from backward.

The second one describes the page number, it also will count from backward if is set to negative. If this parameter is not set, the current page number will be used.

$item = $paginator->getItem(4);
$item = $paginator->getItem(-1, 5);
$item = $paginator->getItem(3, -2);

getItemsByPage()

This method is used to fetch all items of a given page. There must be notice that an adapter should be set first.

$paginator->createAdapter('DbSelect', array());
$items = $paginator->getItemsByPage(7);

getPageRange() and setPageRange()

This two methods are used to operate page range.

$paginator->setPageRange(20);
echo $paginator->getPageRange();

Output:

20

getPages()

This method is used to create pages which include the first page, the previous page, the next page and the last page, etc.

$paginator->getPages('sliding');

getPagesInRange()

This method is used to fetch a subset of pages within a given range.

$pages = $paginator->getPagesInRange(3, 8);

This code will return pages from 3 to 8.

getView() and setView()

These two methods are used to operate the View\\Renderer\\PhpRenderer handler.

render()

This method is used to render the paginator by passed View\\Renderer\\PhpRenderer handler.

setUrlOptions()

This method is used to set options for URL assemble. Parameter type of this method is array, and its fields contains template, pageParam, totalParam, params, router and route.

$paginator->setUrlOptions(array(
    'pageParam'     => 'p',
    'router'        => $this->getEvent()->getRouter(),
    'route'         => $this->getEvent()->getRouteMatch()->getMatchedRouteName(),
    'params'        => array(
        'module'        => $this->getModule(),
        'controller'    => 'index',
        'action'        => 'page',
    ),          
));

In this code, router and route fields are used to generate the url.

buildUrl()

This method is used to build url for a given page number.

$paginator->buildUrl(4);

The example of paginator

Users can use factory() method in Pi\\Paginator\\Paginator to create the handler, the parameter can be an array, an instance of DbTableSelect, DbSelect or Iterator.

Creating an action method and adding the following codes:

$limit = $this->config('item_per_page');
$model = $this->getModel('page');
    
// fetching data from table, and stores into array
$select = $model->select()->where()->order('id');
$rowset = $model->selectWith($select);
$pages = array();
foreach ($rowset as $row) {
    $pages[] = $row;
}

$paginator = \Pi\Paginator\Paginator::factory($pages);
$paginator->setItemCountPerPage($limit);
$paginator->setCurrentPageNumber($page);
$paginator->setUrlOptions(array(
    // Use router to build URL for each page
    'pageParam'     => 'p',
    'router'        => $this->getEvent()->getRouter(),
    'route'         => $this->getEvent()->getRouteMatch()->getMatchedRouteName(),
    'params'        => array(
        'module'        => $this->getModule(),
        'controller'    => 'index',
        'action'        => 'page',
    ),          
));
$this->view()->assign('paginator', $paginator);
$this->view()->setTemplate('page.phtml');

In this code, $limit variable is used to define the number of items to display in a page, it is recommended to allowed user to change this value, so we set this variable as a configuration data and use $this->config('item_per_page'); to fetch the value. Then the data is fetched from table and convert into array type. Finally, several parameters are set for implementing the pagination.

According to the code, we learn that it will request this action when request different pages, so we should add code to process the post data. Adding the following code in front of the above code:

$page = $this->params('p', 5);

This variable tells the action the current page to request.

Now we must add the following code to display the data want to list as well as the paginator:

<h2><?php _e('Paginator'); ?></h2>
<ul>
<?php foreach ($paginator as $article) { ?>
    <li><?php echo $article['title']; ?></li>
<?php } ?>
</ul>

<?php echo $this->paginationControl($paginator, 'Sliding', 'paginator.phtml'); ?>

The paginationControl() will output the paginator, its first parameter is a paginator handler, the second parameter is the scrolling style, the current optional types can be sliding, jumping and elastic, its default value is sliding. The third parameter is the file name which display the paginator, it's already defined by application.