2

I have created joomla pagination for my own component and its working fine for joomla 2.5 and i have use same for joomla 3.0 the data is displaying and also the pagination is also displaying correctly but the issue is when i click on any pagination no. for going next or prev page its not working form remains on same page.

Here is code i have used for creating pagination.

 model.php


    defined('_JEXEC') or die('Restricted access');

    jimport('joomla.application.component.modellist');


     class eventsModelEvents extends JModelLegacy {

     var $_total = null;
     var $_pagination = null;
     function __construct()
     {
            parent::__construct();

            $mainframe = JFactory::getApplication();

            // Get pagination request variables
            $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
             $limitstart = JRequest::getVar('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);
  }
   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;
  }
   function getTotal()
  {
        // Load the content if it doesn't already exist
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);       
        }
        return $this->_total;
  }
  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;
  }
    function _buildQuery()
        {
                // Create a new query object.           
                $db = JFactory::getDBO();
                $query = $db->getQuery(true);
                // Select some fields
                $query->select('*');
                // From the hello table
                $query->from('#__events');
                $query->order('date DESC');
                return $query;
        }
    function getEvents(){
             $db = $this->getDBO();

             $db->setQuery('SELECT * from #__events');
             $events = $db->loadObjectList();

             if ($events === null)
                    JError::raiseError(500, 'Error reading db');

             return $events;
       }
       function getEvent($id){
             $query = ' SELECT * FROM #__events '.
                            ' WHERE id = '.$id;
             $db = $this->getDBO();
             $db->setQuery($query);
             $event = $db->loadObject();          

             if ($event === null)
                    JError::raiseError(500, 'Event with ID: '.$id.' not found.');
             else
                    return $event;          
       }
        function saveEvent($event){

        $db = $this->getDBO();
        $uploaded_path = JPATH_COMPONENT. "/images/";
        if($_FILES["event_image"]["tmp_name"]){
                 if ($_FILES["event_image"]["error"] > 0){
                    return $_FILES["event_image"]["error"] . "<br>";
                } else {
                    move_uploaded_file($_FILES["event_image"]["tmp_name"],$uploaded_path . $_FILES["event_image"]["name"]);                 
                    $event['event_image'] = $_FILES["event_image"]["name"];
                }   

        } else {
            $event['event_image'] = $event['event_stored_image'];

        }


         $event['event_date'] = date('Y-m-d H:i:s', strtotime($event['event_date']));
        foreach($event as $key => $value){
            $event[$key] = mysql_real_escape_string($value);
        }

        if(($event['event_name'] != NULL ) && ($event['event_image'] != NULL)  && ($event['event_date'] != NULL) && ($event['event_description'] != NULL)){

            if(isset($event['event_id'])){
            $query = "UPDATE #__events SET name = '".$event['event_name']."',status =  '".$event['event_status']."',image = '".$event['event_image']."',date =  '".$event['event_date']."',description = '".$event['event_description']."',reservation = '".$event['event_reservation']."' WHERE id =" . $event['event_id']; 
        } else {
            $query = "INSERT INTO #__events (name,status,image,date,description,reservation) VALUES ('".$event['event_name']."','".$event['event_status']."','".$event['event_image']."','".$event['event_date']."','".$event['event_description']."', '".$event['event_reservation']."')"; 
        }

             $db->setQuery($query); 
             if (!$db->query()){
                        $errorMessage = $this->getDBO()->getErrorMsg();
                        JError::raiseError(500, 'Error inserting event: '.$errorMessage);  
               }    
        } else {

             return "Please Fill All fields.";
        }

     }

     function deleteEvents($arrayIDs)
         {
                   $query = "DELETE FROM #__events WHERE id IN (".implode(',', $arrayIDs).")";
                   $db = $this->getDBO();
                   $db->setQuery($query);
                   if (!$db->query()){
                            $errorMessage = $this->getDBO()->getErrorMsg();
                            JError::raiseError(500, 'Error deleting events: '.$errorMessage);  
                   }                  
         }
     function publishEvents($arrayIDs)
         {
                  $query = "UPDATE #__events SET status = '1'  WHERE id IN (".implode(',', $arrayIDs).")"; 
                   $db = $this->getDBO();
                   $db->setQuery($query);
                   if (!$db->query()){
                            $errorMessage = $this->getDBO()->getErrorMsg();
                            JError::raiseError(500, 'Error publishing events: '.$errorMessage);  
                   }                  
         }
     function unpublishEvents($arrayIDs)
         {
                  $query = "UPDATE #__events SET status = '0'  WHERE id IN (".implode(',', $arrayIDs).")"; 
                   $db = $this->getDBO();
                   $db->setQuery($query);
                   if (!$db->query()){
                            $errorMessage = $this->getDBO()->getErrorMsg();
                            JError::raiseError(500, 'Error publishing events: '.$errorMessage);  
                   }                  
         }
}

view.html.php
jimport( 'joomla.application.component.view');

class eventsViewEvents extends JViewLegacy {
    protected $categories;
    protected $items;
    protected $pagination;
    protected $state;
    function display($tpl = null) 

    {



        $this->categories   = $this->get('CategoryOrders');
        $this->state        = $this->get('State');
        $this->addToolBar();
         // Get data from the model
        $events = $this->get('Data');   
        $pagination =$this->get('Pagination');
        // push data into the template
        $this->events = $events;    
        $this->assignRef('pagination', $pagination);
        parent::display($tpl);

    }
    function add($tpl = null){
        $this->addToolBar();


        parent::display($tpl);

    }

        protected function addToolbar()
    {
        require_once JPATH_COMPONENT . '/helpers/events.php';

        $canDo = EventsHelper::getActions($this->state->get('filter.category_id'));
        $user = JFactory::getUser();
        JToolBarHelper::title('Event Manager', 'generic.png');
        JToolBarHelper::addNew('add');
        if (count($user->getAuthorisedCategories('com_events', 'core.create')) > 0)
        {
            //JToolBarHelper::addNew('add');
        }

        if (($canDo->get('core.edit')))
        {
            JToolBarHelper::editList('edit');
        }

        if ($canDo->get('core.edit.state'))
        {
            if ($this->state->get('filter.state') != 2)
            {
                JToolBarHelper::divider();
                JToolBarHelper::publish('publish', 'JTOOLBAR_PUBLISH', true);
                JToolBarHelper::unpublish('unpublish',  'JTOOLBAR_UNPUBLISH', true);
            }


        }



        if ($this->state->get('filter.state') == -2 && $canDo->get('core.delete'))
        {
            JToolBarHelper::deleteList('', 'remove', 'JTOOLBAR_EMPTY_TRASH');
            JToolBarHelper::divider();
        }
        elseif ($canDo->get('core.edit.state'))
        {
            JToolBarHelper::trash('remove');
            JToolBarHelper::divider();
        }
    }

    function displayEdit($eventId,$tpl = NULL)
    {                            

       JToolBarHelper::title('Event'.': [<small>Edit</small>]');
       JToolBarHelper::save();
       JToolBarHelper::cancel();  

       $model = $this->getModel();
       $event = $model->getEvent($eventId);
       $this->event = $event;

       parent::display($tpl);
    }
     function displayAdd($tpl = NULL){
             JToolBarHelper::title('Event'.': [<small>Add</small>]');
             JToolBarHelper::save();
             JToolBarHelper::cancel();  


             parent::display($tpl);
    }

    }

     default.php

     <td colspan="9"><?php echo $this->pagination->getListFooter(); ?></td>

Can any help me what is wrong or what i am missing

Jitender Thakur
  • 490
  • 5
  • 15

2 Answers2

1

This might be because the required Javascript frameworks aren't available. To ensure if this is the case, you can check your javascript console.

If that is the case, in your view extending JViewLegacy, before the line:

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

Insert below line:

JHtml::_('behavior.framework');

Also, make sure your template is not removing the required frameworks.

unset($doc->_scripts[JURI::root(true) . '/media/system/js/core.js']);

Comment out this line if you see it in your template index.php

Hope this helps :)

MicroWise
  • 416
  • 3
  • 5
0

Extend class JModelList instead of JModelLegacy, that should sorry it out.

Brian Bolli
  • 1,873
  • 1
  • 12
  • 14
  • I have also tried JModelList but nothing change. same issue – Jitender Thakur Mar 28 '14 at 05:58
  • Try removing the method getPagination() from your model class. You are overriding and therefor voiding all the work already done for you by the core pagination functionality by overriding the getPagination() method from the JModelList class. You also need to rename your _buildQuery() method to getListQuery() to ensure the parent classes have access to the raw data to paginate. – Brian Bolli Mar 28 '14 at 14:14
  • I have removed getPagination() and also renamed _buildQuery() to getListQuery() but still no effect. same blank page but when i remove the '__construct()' everything working fine but without pagination. – Jitender Thakur Apr 10 '14 at 18:37