正与很多其他的joomla组件一样,我们的组件也需要对记录进行分页。在本节中,就要解决这个问题。
joomla已经为我们实现了一个分页的类---JPagination.这个类的实现文件是/libraies/joomla/html/pagination.php。
这个类的构造函数需要3个参数,分别是:
$total 需要显示的记录的总数
$limitstart 从哪一页开始显示
$limit 每页显示的数量
只要能够准确的提供这3个变量,那么就可以成功在我们的组件中实现分页功能。
修改模型文件messagelist.php
//后台的messagelist的模型类 defined('_JEXEC') or die('Restricted Access'); jimport('joomla.application.component.model'); //jimport('joomla.application.component.modellist'); class GuestbookModelMessageList extends JModel { /** * Items total * @var int */ 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::getInt('limitstart',0); // In case limit has been changed ,adjust it $limitstart = ($limit!=0?(floor($limitstart / $limit)*$limit):0); $this->setState('limit',$limit); $this->setState('limitstart',$limitstart); } /** * Get a Pagination Object * * @access public * @return JPagination */ function getPagination() { // Load the content if it doesn't exist if(empty($this->_pagination)) { jimport('joomla.html.pagination'); $this->_pagination = new JPagination($this->getTotal(),$this->getState('limitstart'),$this->getState('limit')); } return $this->_pagination; } /** * Get Number of items * * @access public * @return interger */ function getTotal() { // load the content if it does't already exist if(empty($this->_total)) { $query = $this->getListQuery(); //JModel::_getListCount Return a record count for the query $this->_total = $this->_getListCount($query); } return $this->_total; } /** * 从数据库中获取数据 * * @access public * @return messages * */ function getMessages() { $query = $this->getListQuery(); $db = & JFactory::getDBO(); $db->setQuery((string)$query); // JModel::_getList Get an array of objects from the result of database query // _getList($query,$limitstart = 0, $limit = 0) $messages = $this->_getList($query,$this->getState('limitstart'),$this->getState('limit')); return $messages; } function getListQuery() { $db = & JFactory::getDBO(); $query = $db->getQuery(true); $query->select("*")->from( $db->nameQuote('#__guestbook')); return $query; } function publishMessage() { $cids = JRequest::getVar('cid',array(0),'post','array'); //将数组变为字符串的形式,以","分割 $idstr = implode(",",$cids); $db = &JFactory::getDBO(); $query = $db->getQuery(true); $query->update($db->nameQuote('#__guestbook'))->set($db->nameQuote('message_publish')." = 1 ")->where($db->nameQuote('message_id')."in (".$idstr.")"); $db->setQuery((string)$query); //既然成功执行了 ,为什么还有一个警告 //$db->query()方法成功返回 查询的结果 失败返回false if(!$db->query()) { return false; } return true; } }
相比以前的代码,我们只做了少许改动。目的就是要成功的实例化一个JPagination对象。
首先,增加了两个变量,$_total,用来表示共有的记录数。$_pagination表示分页对象。并且我们也实现了构造函数。在构造函数中:
$limit = $mainframe->getUserStateFromRequest('global.list.limit','limit',$mainframe->getCfg('list_limit'),'int'); 这段代码得到每页显示的多少,首先在URL中检查,如果找不到,就用系统默认配置的值。
得到每页的显示的数目,和开始显示的页数后,我们将这个两个值放到了seesion中,储存起来。
$this->setState('limit',$limit);
$this->setState('limitstart',$limitstart);
这个两行代码正是完成这个功能。
由于在多个地方使用到了查询语句,因此单独写一个getListQuery()函数,将它封装起来。
最后,在getPagination中new 一个JPagination对象并返回该对象。
修改了模型,也要对视图类做一点修改:
function display($tpl = null) { // add required JS JHtml::_('behavior.framework'); $this->pagination = $this->get('Pagination'); $messages = & $this->get('Messages'); $this->assignRef('messages',$messages); // add toolbar $this->addToolBar(); //display the template parent::display($tpl); }
在视图类中,我们首先加载必要的js文件。JHtml::_('behavior.framework');就是完成这项工作。然后调用get方法得到JPagination对象。
完成这些之后,最后需要做的就是修改视图模板。在模板中显示分页,需要修改的地方也很少,我们只需要在table中再加一行,就行了。
<tfoot>
<td colspan="8">
<?php echo $this->pagination->getListFooter();?>
</td>
</tfoot>
这样就完成了。getListFooter方法是Jpagination提供的一个方法,她将产生出我们需要的分页效果。
看一下实现后的效果:
至此分页功能已成功实现。当然在J25中实现分页功能其实不用这么麻烦的。我们只需要要让我们的类直接派生自JModelList就自动实现了Jpagination类了。
以后再做介绍。
评论 (0)