Implementing pagination on the list view

In order to implement the Pagination on the frontend breeds list, you have to update the following files.

update breeds.php /site/models/breeds.php

update view.html.php /site/views/breeds/view.html.php

update default.php /site/views/breeds/tmpl/default.php

File Details

/site/models/breeds.php

protected function populateState()
{
		// Initialise variables.
 $app = JFactory::getApplication();
		
 $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'));
 $this->setState('list.limit', $limit);

 $limitstart = $app->getUserStateFromRequest('limitstart', 'limitstart', 0);
 $this->setState('list.start', $limitstart);

}

Customizing the populateState method

What we are first doing in the Breeds model class is, we are calling the parent populate state method that resides in the JModelList and passing it the information about pagination, which is the maximum limit of the results to be displayed on a page and secondly the starting point of the list or you can say that from which point the results list should start.  At the start, we have initialized the $app variable for holding the Joomla application object. This application object is the same thing that pulls and puts together the components, modules, plugins and templates to make our complete web page. We get this object through getApplication method, belonging to JFactory method.

If you have so many records in a specific database table and are populating the results in the list view, then if you are on page 1 of the list, the “limitstart” value would be 0 and if you have selected to display 5 results per page and you are on page 2, the limitstart value will become 5. So you can say that $limitstart specifies which item is the first to be displayed on the current page. So, in our populatestate method, the variables, $limit and $limitstart are getting these two values respectively through “getUserStateFromRequest” method.

This feature enables Joomla to record the state or value of a particular variable, it is getting from request and to handle this, there is a state of the art method, called, “getUserStateFromRequest“. This behaves like a very useful method belonging to JApplication class and comes with a number of arguments. Starting from the $limit property on line 21, the first argument passed to the method is, the path to the value in the state data. It is mentioned here as , “global.list.limit” because this value is not component specific and will globally remain the same in Joomla. the second argument is the input request name from which the value will be updated. The last argument, $app->get(‘list_limit’) provides the default value for pagination limit and it picks this from main configuration file. If you want to override all of this stuff, you can simply assign the limit variable, an integer value like,  $limit = 20;. The arguments for the $limitstart are also self explanatory, we are getting the values from limitstart variable and passing 0 as a default offset value.

You can easily change the list limit from backend, by logging in to backend as super user and then going to System –> Global Configuration  –> Site Settings and then set the Default List Limit.

Joomla extension list limit

So, on the basis of this, you can say that through “populateState” method, we can easily get the information from the request in the model. The JModelList class also provides several other request variables that make us to interact with lists more easily such as column ordering and direction, listing page length etc.

/site/views/breeds/view.html.php

$this->pagination = $this->get('Pagination');

Add the code mentioned on line 4 to the view.html.php file at the same position as shown in the above code. This line of code will fetch the pagination object from model and assign it so that it can vbe used in the template file.

/site/views/breeds/tmpl/default.php

        <tfoot>
		<tr>
			<td colspan="<?php echo isset($this->items[0]) ? count(get_object_vars($this->items[0])) : 10; ?>">
				<?php echo $this->pagination->getListFooter(); ?>
			</td>
		</tr>
		</tfoot>

In the breeds list default.php file, we are adding the above mentioned code after the closing tbody section of table. We are closing the pagination code in the tfoot section and on line 8, we have added some php code to calculate number of columns it has to calculate for proper rendering of tfoot section according to the table body and header on the basis of setting the column span. After that, there is a  php code for rendering paginations buttons by calling getListFooter() method through pagination object.

Joomla Pagination

Download Code

Next –> Implementing Grid sort functionality at frontend

Add a Comment

Your email address will not be published. Required fields are marked *

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com