J1.5

Using JPagination in your component

From Joomla! Documentation

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.

Class Overview

The JPagination class, introduced in Joomla! 1.5, allows developers to reliably and consistently add 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.

getListFooter()

Returns a combination of the several page-related elements, including: the Display Limit dropdown, the Pages Links and the Pages Counter. Appearance differs in the Front-end and Back-end due to additional CSS formatting applied with the Khepri template.

Front-end:

Back-end:

getLimitBox()

/** * Creates a dropdown box for selecting how many records to show per page * * @access public * @return string The html for the limit # input box * @since 1.0 */functiongetLimitBox()

Returns an HTML string that will output the Display Limit dropdown as

orderUpIcon()

orderDownIcon()

Examples

with JDatabase

Here is a nice method that uses the strength of mysql who knows pagination too. Really!

Most developers don't use the SQL_CALC_FOUND_ROWS and just double the query without limit. Just don't!! ;-)

$db=&JFactory::getDBO();$lim=$mainframe->getUserStateFromRequest("$option.limit",'limit',14,'int');//I guess getUserStateFromRequest is for session or different reasons$lim0=JRequest::getVar('limitstart',0,'','int');$db->setQuery('SELECT SQL_CALC_FOUND_ROWS x, y, z FROM jos_content WHERE x',$lim0,$lim);$rL=&$db->loadAssocList();if(empty($rL)){$jAp->enqueueMessage($db->getErrorMsg(),'error');return;}else{////Here the beauty starts$db->setQuery('SELECT FOUND_ROWS();');//no reloading the query! Just asking for total without limitjimport('joomla.html.pagination');$pageNav=newJPagination($db->loadResult(),$lim0,$lim);foreach($rLas$r){//your display code here}echo$pageNav->getListFooter();//Displays a nice footer

Implementation

Changes to the Model

Declare $_total and $_pagination variables in the model; these will be returned by the functions getTotal() and getPagination(), respectively.

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.

functiongetTotal(){// Load the content if it doesn't already existif(empty($this->_total)){$query=$this->_buildQuery();$this->_total=$this->_getListCount($query);}return$this->_total;}

Create a getPagination() function. The function will create and return a new Pagination object that can be accessed by the View.

functiongetPagination(){// Load the content if it doesn't already existif(empty($this->_pagination)){jimport('joomla.html.pagination');$this->_pagination=newJPagination($this->getTotal(),$this->getState('limitstart'),$this->getState('limit'));}return$this->_pagination;}

Changes to the View

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

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.

classComponentnameModelModelNameextendsJModelList{/** * Method to build an SQL query to load the list data. * * @return string An SQL query */protectedfunctiongetListQuery(){// Create a new query object. $db=JFactory::getDBO();$query=$db->getQuery(true);// Select some fields$query->select('*');// From the tablename$query->from('#__tablename');return$query;}}

Most of the hard work is already done for you by Joomla!. You only have to extend the above class and add the getListQuery() method that helps you build the query in which the pagination will be based.

Pagination methods, like getPagination(), getItems() & getListFooter() that will be called later from your view are already defined in the parent class of the subclass above.

Content is available under Joomla! EDL unless otherwise noted.Joomla!® is a registered trademark of Open Source Matters, Inc.If you are experiencing technical issues with this website, please report it
.
Notice a content problem? Create an account
and fix it.