Using JPagination in your component
From Joomla! Documentation
Revision as of 11:54, 13 February 2008 by Radiant tech (talk | contribs)
The "J1.5" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Please Note: This page has not been completed and instructions below apply specifically to adding Pagination in the Back-end. Additional instructions for the Front-end will be added soon.
Introduction[edit]
The JPagination class, introduced in Joomla! 1.5, allows developers to reliably and consistently adding pagination to the Front-end and Back-end display of their components. The file containing the class can be found at /libraries/joomla/html/pagination.php. The pagination object generates start/end, previous/next, and current page buttons as shown in the image below.
Changes to the Model[edit]
Declare $_total and $_pagination variables in the model; these will be returned by the functions getTotal() and getPagination(), respectively.
/** * Items total * @var integer */ var $_total = null; /** * Pagination object * @var object */ var $_pagination = null;
Add to or create a __construct() function that will establish values for the $limitstart and $limit variables as these are needed by the JPagination class.
function __construct() { parent::__construct(); global $mainframe, $option; // Get pagination request variables $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int'); $limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int'); // In case limit has been changed, adjust it $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0); $this->setState('limit', $limit); $this->setState('limitstart', $limitstart); }
Revise the getData() function, adding the $limitstart and $limit values to the _getList() query. This causes only the needed rows to be returned, rather than all rows.
function getData() { // if data hasn't already been obtained, load it if (empty($this->_data)) { $query = $this->_buildQuery(); $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit')); } return $this->_data; }
Create a getTotal() function. This function uses the _getListCount() method from JModel to return the total number of rows in the query. The value returned will be used by the getPagination() function.
function getTotal() { // Load the content if it doesn't already exist if (empty($this->_total)) { $query = $this->_buildQuery(); $this->_total = $this->_getListCount($query); // _getListCount is from JModel class } return $this->_total; }
Create a getPagination() function. The function will create and return a new Pagination object that can be accessed by the View.
function getPagination() { // Load the content if it doesn't already exist if (empty($this->_pagination)) { jimport('joomla.html.pagination'); $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') ); } return $this->_pagination; }
Changes to the View[edit]
Revise the View to obtain the pagination object created in the Model and assign it for use in the template.
... // Get data from the model $items =& $this->get('Data'); $pagination =& $this->get('Pagination'); // push data into the template $this->assignRef('items', $items); $this->assignRef('pagination', $pagination); ...
Changes to the Template[edit]
Add a footer area to the display table in the template which holds the pagination object. The method getListFooter() from the JPagination class generates the buttons and their next/previous functionality as shown in the image above. Edit colspan="9" to reflect the number of columns in the table.
... <tfoot> <tr> <td colspan="9"><?php echo $this->pagination->getListFooter(); ?></td> </tr> </tfoot> ...