Data Paginator - Jet\Data_Paginator

This class helps to solve a classic (and often "popular") problem with paging of list items. Be careful to avoid misunderstanding. The class does not solve UI rendering at all. Jet has another set of classes for easy implementation of the entire list handling interface, including filters, as well as for rendering the list itself. This class handles the model - that is, the logic and algorithm for paging the data.

It is possible to page both a list in a regular array and an instance of some iterator, which must implement the Jet\Data_Paginator_DataSource class.

The page builder assumes that the data about the number of items per page and also the current page number will be received on the input - directly as constructor parameters. This means that the class doesn't even care about capturing input (such as part of a URL, GET parameter, and so on). That is the responsibility of your controller. So the class really only "handles" the relevant paging algorithm and that makes it completely versatile, all-purpose and very useful.

Method Overview

Method Meaning of
public __construct(
int $current_page_no,
int $items_per_page,
callable $URL_creator
)
The pager must be initialized correctly when the instance is created. Let's break down the parameters:
  • $current_page_no
    Current page number. This must be captured from the input and passed to the controller that creates the paging instance.

  • $items_per_page
    Number of items per page. Attention! Must not be greater than the limit specified by system configuration - SysConf_Jet_Data_Paginator.

  • $URL_creator
    The page builder also has the task of generating the URL of the list pages. However, it doesn't know itself what the URL of the page should look like - this is still a matter for the controller. So it needs a URL generator/creator, to which it passes the page number and it takes care of generating the URL.

    The anonymous generator function: function( int $page_no ) : string
    {
        return 
    '...some url ...'.$page_no;     
    };
public setURLCreator(
callable $URL_creator
) : void
If necessary, the generator/creator (see class constructor) can be replaced even after the class instance is created.
public setData(
array|Data_Paginator_DataSource $data
) : void
The method is used to pass data that are intended for paging - i.e. the whole list. It can be a simple pol, but it can also be a data source. That is, an instance of a class that implements the required interface.

In practice, the latter is more common, and for example DataModel interacts directly with the paging engine.
public setDataSource(
Data_Paginator_DataSource $data
) : void
See the setData method. The setDataSource method has the difference that it does not accept a plain array, but only an iterator instance.
public getData(
) : iterable
It will return the result. Thus:
  • If the input data is an array, then its corresponding section is returned - the part of the array corresponding to the given page
  • If the input is an iterator, then it is set - its corresponding setPagination method is called and for the sake of order its instance is returned as the return value.
public getDataItemsCount(
) : int
Returns the total number of records in the original list of items.
public getPagesCount(
) : int
Returns the number of pages.
public getCurrentPageNo(
) : int
Returns the current page number.
public getPrevPageNo(
) : int|null
Returns the previous page number. If there is no previous page (the current one is the first), then it returns null.
public getNextPageNo(
) : int|null
Returns the following page number. If there is no next page (the current one is the last), then it returns null.
public getDataIndexStart(
) : int
Returns the initial data index of the current page - in the sense of working with an array.
public getDataIndexEnd(
) : int
Returns the final data index of the current page - in the sense of working with an array.
public getShowFrom(
) : int
Returns the data for the user: displayed item from: X (returns the value X)
public getShowTo(
) : int
Returns the user specified value: item displayed to: X (returns the value of X)
public getCurrentPageNoIsInRange(
) : bool
Indicates whether the passed valid page number is in the range.

It can happen that the page number 11 was passed, but there are only 10 pages in reality. In this case the method returns false, otherwise true.
public getPrevPageURL(
) : null|string
Returns the URL of the previous page. If there is no previous page (the current one is the first), then it returns null.
public getNextPageURL(
) : null|string
Returns the URL of the following page. If there is no next page (the current one is the last), then it returns null.
public getLastPageURL(
) : null|string
Returns the URL of the last page.
public getFirstPageURL(
) : null|string
Returns the URL of the first page.
public getPagesURL(
) : array
Returns the URL of all pages.
public toJSON(
) : string
Translates paging information into JSON format.
public jsonSerialize(
) : array
It is possible to pass the json_encode function to the paging device.
Previous chapter
Jet\Data_Forest
Next chapter
Jet\Data_Paginator_DataSource