Skip to content
Tim Curzon edited this page Feb 28, 2022 · 2 revisions

Ztal 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.


__construct (array) : void

The constructor takes an optional options array which currently supports the following:

  • rowsPerPage
  • currentPage

initWithParameters (array, string{''}) : void

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.


isMultipage () : boolean

This method returns true of the total count of rows is greater than the number of rows per page.


pages () : array

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.


previousPage () : int

return the index of the previous page. A value of -1 means there is no previous page.


nextPage () : int

return the index of the next page. A value of -1 means there is no next page.


getTotalRowCount () : int

Return the total number of rows in the table before pagination.


getCurrentPage () : int

Return the current page index.


setCurrentPage (int) : void

Set the page the paginator considers the current page. Setting an index outside of the valid range is ignored.


getRowsPerPage () : int

Return the number of rows shown per page.


setRowsPerPage (int) : void

Set the number of rows that will be displayed on a page. Setting a value less than 1 will be ignored.


_sliceDataSource (&mixed, int, int) : void : throws Exception

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.

Clone this wiki locally