2014-03-27 106 views
1

我爲我自己的組件創建了joomla分頁,並且它爲joomla 2.5工作正常,並且我使用了相同的joomla 3.0數據顯示,並且分頁也正確顯示,但是問題是當我點擊任何分頁號。對於下一頁或上一頁,其不工作的表單仍保留在同一頁上。Joomla分頁不適用於joomla 3.0

這裏是我用於創建分頁的代碼。

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> 

任何能幫我什麼是錯還是什麼我缺少

回答

0

擴展類JModelList代替JModelLegacy,應該不好意思出來。

+0

我也曾嘗試JModelList但沒有任何變化。同樣的問題 –

+0

嘗試從您的模型類中移除方法getPagination()。您重寫並因此通過重寫JModelList類中的getPagination()方法來排除核心分頁功能已爲您完成的所有工作。您還需要將_buildQuery()方法重命名爲getListQuery(),以確保父類可以訪問原始數據以進行分頁。 –

+0

我已經刪除了getPagination()並且還將_buildQuery()重命名爲getListQuery(),但仍然沒有效果。相同的空白頁面,但當我刪除'__construct()'一切工作正常,但沒有分頁。 –

1

這可能是因爲所需的Javascript框架不可用。爲了確保是這種情況,你可以檢查你的javascript控制檯。

如果是延長JViewLegacy的情況下,在您看來,前行:

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

插入下面一行:

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

此外,請確保您的模板不刪除所需的框架。

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

註釋掉這行,如果你看到它在你的模板index.php

希望這有助於:)

+0

哇..我剛剛看到了**日期**的問題..呃..讓我們希望它可以幫助別人.. – MicroWise