Joomla Pagination

时间:2014-02-09 14:18:58

标签: php joomla pagination

我已在组件前端列表模板中成功实现了分页框。然而,当我尝试设置列出项目的限制时,它将无法工作,我想知道错过了什么。

模型中的

  var $_total = null;

  /**
   * Pagination object
   * @var object
   */
  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 _buildQuery(){
         $query = ' SELECT * '
                 . ' FROM #__event '
                 .'Where published = 1'
            ;

         return $query;
    }

      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 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 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;
    }
views / view.html.php中的

(本文档的完整版)

 class EventViewListing extends JViewLegacy
{ 
// Overwriting JView display method
function display($tpl = null) 
{

            $model= & JModelLegacy::getInstance('Event','EventModel');
            $pagination = $model->getPagination();
            $this->assignRef('pagination',   $pagination);



  $JDoc =& JFactory::getDocument();
  $db = JFactory::getDBO();

  $sql = "SELECT * FROM #__event WHERE published = 1 ORDER BY id DESC";
  $db->setQuery($sql);
  $rows = $db->loadObjectList();


  $sql2 = "SELECT * FROM #__user_usergroup_map WHERE group_id = 5 or group_id = 8";
  $db->setQuery($sql2);
  $rows2 = $db->loadObjectList();


   $this->assignRef('rows',$rows);
   $this->assignRef('rows2',$rows2);


 //  $JDoc->setTitle('  ');

    // Display the view
    parent::display($tpl);
}
}
default.php中的

<form action="<?php echo JRoute::_('index.php?option=com_event'); ?>" method="post" name="adminForm">


<?php echo $this->pagination->getListFooter(); ?>
<input type="submit" name="submit" value="GO!"  /> 
</form>
希望有人可以提供帮助 谢谢!

1 个答案:

答案 0 :(得分:4)

还需要在查询中使用限制来限制显示在您所在页面上的记录数。通常,这可以在setQuery函数中完成,该函数允许第二个和第三个参数设置限制大小和起始位置。

  $sql = "SELECT * FROM #__event WHERE published = 1 ORDER BY id DESC";
  $db->setQuery($sql, $model->getState('limitstart'), $model->getState('limit'));
  $rows = $db->loadObjectList();


  // I'm not sure what this query is for, but since it probably isn't supposed to be limited to a set number of items, don't update it's setQuery call.
  $sql2 = "SELECT * FROM #__user_usergroup_map WHERE group_id = 5 or group_id = 8";
  $db->setQuery($sql2);
  $rows2 = $db->loadObjectList();

我认为这可以解决您遇到的问题。


话虽这么说,但是你有很多小问题让你更难或只是使用过时的做法:

 $limitstart = JRequest::getVar('limitstart', 0, '', 'int');

不推荐使用JRequest :: getVar(),可能会在将来的Joomla版本中删除。而是使用它:

 $limitstart = JFactory::getApplication()->input->get('limitstart', 0, 'INT');

请注意,参数略有变化。这使用不同的类来解析应用程序的输入。

$this->assignRef('rows',$rows);

上面的内容已经不再需要了(仅根据我的理解需要在PHP4中使用)。而只是做$this->rows = $rows;

最后,最重要的问题是你并没有真正使用Joomla的帮助。

您的模型应该只是从类JModelList扩展,因为您正在尝试创建事件列表。如果您从该类扩展并正确命名您的函数,Joomla将完成大部分工作:

  1. _buildQuery重命名为getListQuery

  2. 几乎删除模型中的所有其他函数,因为Joomla在JModelList中的所有函数基本上都是相同的。

  3. 将您的观点更新为:

    class EventViewListing extends JViewLegacy
    { 
    // Overwriting JView display method
    function display($tpl = null) 
    {
    
      $JDoc = JFactory::getDocument();
      $db = JFactory::getDBO();
    
      $this->pagination = $this->get('Pagination');
      $this->rows = $this->get('Items');
    
      $sql2 = "SELECT * FROM #__user_usergroup_map WHERE group_id = 5 or group_id = 8";
      $db->setQuery($sql2);
      $this->rows2 = $db->loadObjectList();
    
      //  $JDoc->setTitle('  '); 
    
         // Display the view
         parent::display($tpl);
     }
     }
    
  4. JViewLegacy类中的

    $this->get()将调用模型(与视图同名)并运行该模型的方法,该方法以get开头,后跟函数中的任何单词,因此$this->get('Pagination')调用模型getPagination函数。

    同样,您在模型中添加的所有功能都已存在于libraries/legacy/model/list.php中,所以只需使用它们即可!