-
Notifications
You must be signed in to change notification settings - Fork 3
Table paginator
Ztal\Table\Paginator\Abstract and its two concrete subclasses manage the process of tracking the current page, the number of rows to display on a page and the data set that represents these. The Array concrete subclass uses array_slice to prepare the data source while the Object concrete subclass relies on a method called slice that needs to be present in the Object used as the data source.
In most cases the Array subclass for the paginator should be all you will need because even when the data for each row in a table is an object the set of row data objects is often gathered into an array. However, if a container object is used to manage the individual rows/records then either the Object subclass or a new subclass can be used to interact with that container object.
The constructor takes an optional options array which currently supports the following:
- rowsPerPage
- currentPage
initWithParameters expects to receive an array of GET / POST parameters from which it can pull the values needed to set the current state of the table. At the moment only the 'page' key is used. The second (optional) parameter is used to supply a prefix string that is used to prefix all the keys in the array related to the current table. This is useful if there are multiple tables on a page as each may have a different prefix.
This method returns true of the total count of rows is greater than the number of rows per page.
The pages method returns an array detailing each page available.
<?php
array(
array(
'index' => 0,
'label' => 1,
'currentPage' => true,
),
array(
'index' => 1,
'label' => 2,
'currentPage' => false,
),
)The label is currently just the index + 1 but could be used to provide alternative labelling for pages.
return the index of the previous page. A value of -1 means there is no previous page.
return the index of the next page. A value of -1 means there is no next page.
Return the total number of rows in the table before pagination.
Return the current page index.
Set the page the paginator considers the current page. Setting an index outside of the valid range is ignored.
Return the number of rows shown per page.
Set the number of rows that will be displayed on a page. Setting a value less than 1 will be ignored.
This is the method called to actually set the range of data used when rendering a single page of data. This method should be overridden by most subclasses. The first parameter is a reference to the data source. The second parameter is the start index and the third parameter is the count of the number of data items needed.
The Abstract class throws an Exception if this method is called.
The Array subclass uses array_slice to reduce the data source to one page's worth of data.
The Object subclass calls a method called slice with 2 parameters: a start integer and a count integer.
There is no requirement to actually slice the data source or otherwise perform destructive actions. As long as the container object behaves as if the data it holds has actually been sliced.