简体   繁体   English

Joomla分页

[英]Joomla Pagination

I have successfully implemented the pagination box in a component front end listing template. 我已经在组件前端列表模板中成功实现了分页框。 however, when i try to set the limit of listing items, it won't work, i wonder what is missed out. 但是,当我尝试设置列表项的限制时,它将不起作用,我想知道错过了什么。

in the model 在模型中

  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;
    }

in the views/view.html.php (full version of this document) 在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);
}
}

in the default.php 在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>

hope someone could help thank you! 希望有人能帮助您!

The limits need to also be used in the queries to limit the number of records that are displayed to the page that you are on. 这些限制也需要在查询中使用,以限制显示到您所在页面的记录数。 Typically this can be done in the setQuery function, which allows a second and third parameter to set the limit size and start position. 通常,这可以在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();

I think that fixes the problem that you are having. 我认为这可以解决您遇到的问题。


That being said, you have a host of minor issues that are making this a lot harder for you or just using outdated practices: 话虽这么说,但是您有许多小问题,这使您的工作变得更加困难,或者只是使用过时的做法:

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

Using JRequest::getVar() is deprecated and likely to be removed in future versions of Joomla. 不推荐使用JRequest :: getVar(),并且可能在以后的Joomla版本中将其删除。 Instead use this: 而是使用此:

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

Note that the parameters have changed slightly. 请注意,参数略有变化。 This uses a different class to parse input to the application. 这使用不同的类来解析对应用程序的输入。

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

The above is unnecessary anymore (was only needed back in PHP4 from what I understand). 上面的内容不再是必需的(据我所知,仅PHP4才需要使用)。 Instead just do $this->rows = $rows; 相反,只需执行$this->rows = $rows;

Finally, the big overall issue is that you aren't really using Joomla's help. 最后,总的来说最大的问题是您并没有真正使用Joomla的帮助。

Your model should just be extending from the class JModelList since you are trying to create a list of events. 您的模型应该只是从类JModelList扩展而来,因为您试图创建事件列表。 If you extend from that class and name your functions properly, Joomla will do most of the work: 如果从该类扩展并正确命名函数,则Joomla将完成大部分工作:

  1. Rename _buildQuery to getListQuery . _buildQuery重命名为getListQuery

  2. Pretty much delete every other function in your model, since Joomla has all of them in JModelList doing basically the same things. 几乎删除了模型中的所有其他函数,因为Joomla在JModelList中将所有这些函数做的基本上相同。

  3. Update your view to this: 更新您的视图:

     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); } } 

$this->get() in the JViewLegacy class will call the model (with the same name as the view) and run the method of that model that starts with get followed by whatever word is in the function, so $this->get('Pagination') calls the models getPagination function. JViewLegacy类中的$this->get()将调用模型(与视图名称相同),并运行该模型的方法,该方法以get开头,后跟函数中的任何单词,因此$this->get('Pagination')调用模型的getPagination函数。

And again, all of the functions that you are adding in the model exist already in libraries/legacy/model/list.php , so just use them! 同样,您要在模型中添加的所有功能都已经存在于libraries/legacy/model/list.php ,因此只需使用它们即可!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM