Pagination in Your Joomla Component

While I was working today to fix how to sort the pagination in my Joomla component, I found this way of pagination to work for me. Hope this will help you too   First of all I edited my Model which interacts with database. I made a function to display some content from the database which is as follows

LOCATION /com_findengg/models/findengg.php

 function displayDeadRegions()
/* THIS CODE IS TO BE MODIFIED AS PER YOUR MODEL. THE ONLY THING I CHANGED IN MY MODEL IS I ADDED $mainframe variable $limit  $limitstart and modified the query */

{
   global $mainframe;
   $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' ); 
   $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' ); 
   $db =& JFactory::getDBO();
   $query = 'SELECT * FROM `jos_findmyengineer_dead` ORDER BY `jos_findmyengineer_dead`.`date_logged` DESC ';
   $db->setQuery( $query, $limitstart, $limit );
   if( !$db->query() ) 
      { $errorMessage = $this->getDBO()->getErrorMsg();
        JError::raiseError( 500, 'Error in GETTING List of Dead RegionS: ' . $errorMessage ); 
      } 
   $dead_regions = $db->loadObjectList(); // Return the list of product types return $dead_regions; }///end of function dead regions  

In the same model I made another function to get the page parameters

LOCATION /com_findengg/models/findengg.php


  function getPageNavParameters($tablename) {
    global $mainframe;
    $db =& JFactory::getDBO(); 
    $table = $db->nameQuote( $tablename ); 
    $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' ); 
    $limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' ); 
    $query = "SELECT COUNT(*) FROM " . $table; 
    $db->setQuery( $query ); 
    $total = $db->loadResult();
    jimport('joomla.html.pagination');
    $pageNav = new JPagination( $total, $limitstart, $limit ); 
    return $pageNav; 
    }//end of function getPageNavParameters 

Now I edited the view.html.php file. The place where I was making call to my layout, I called this function as follows

LOCATION /com_findengg/views/findengg/view.html.php

 
          $layout = JRequest::getVar( 'layout', 'default' ); 
          $this->setLayout($layout); 
          switch ($layout) { 
            case deadregions:{ 
                              $dead_regions=$model->displayDeadRegions();
                              $this->assignRef( 'dead_regions', $dead_regions);
                              $db_table_name='jos_findmyengineer_dead'; 
                              $pageNav=$model->getPageNavParameters($db_table_name);
                              $this->assignRef( 'pagination', $pageNav); 
                              break; } 
            default:{ 
                            } 
       } parent::display( );
    }
 } 

Finally in my template layout file in folder tmpl I added this code in the table

LOCATION /com_findengg/views/tmpl/deadregions.php

 	
	 echo pagination->getListFooter(); 
Advertisements

3 comments

  1. Thanks for the above code. I hope someday they will make Joomla pagination as easy to work with as WordPress pagination. WordPress is light years ahead when it comes to simpler code…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s