本节将会为我们的组件增加排序功能。
要实现排序功能,我们需要知道两个东西,一个是根据哪一个字段来排序,另一个是以什么顺序来排序。
首先解决第一个问题,根据哪一个字段来排序?
在joomla中,我们可以用JHtml::_('grid.sort','序号','message_id',$listDirn,$listorder)来标记需要排序的字段。
第一个参数'grid.sort'是固定的。不能改变。
第二个参数是标题名。这个随便写。
第三个参数是数据库中对应的字段名.这个必须和数据库中的字段名相对于,否则出错。
第四个参数是以什么顺序。我们这里用一个$listDirn变量来表示。
第五个参数是要排序的栏。用$listorder表示(这里可能理解有误)
那么为了让我们的组件可以排序排序,首先来修改一下视图模板,标记可以排序的字段。
修改administrator\componets\com_guestbook\views\messagelist\tmpl\default.php文件
<?php
//后台留言板显示留言界面
defined('_JEXEC') or die('你不能直接访问这个文件!');
$listOrder = $this->sortColumn;
$listDirn = $this->sortDirection;
?>
<form action="index.php" method="post" name="adminForm">
<div id="editcell">
<table class="adminlist">
<thead>
<tr>
<th width="20px">
<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($this->messages);?>)" />
</th>
<?php
$id = JHtml::_('grid.sort','序号','message_id',$listDirn,$listOrder);
$subject = JHtml::_('grid.sort','主题','message_subject',$listDirn,$listOrder);
$content = JHtml::_('grid.sort','正文','message_content',$listDirn,$listOrder);
$author = JHtml::_('grid.sort','作者','message_author',$listDirn,$listOrder);
$email = JHtml::_('grid.sort','Email','message_author_email',$listDirn,$listOrder);
$publish = JHtml::_('grid.sort','发布','message_publish',$listDirn,$listOrder);
$time = JHtml::_('grid.sort','时间','message_time',$listDirn,$listOrder);
?>
<th> <?php echo $id;?> </th>
<th> <?php echo $subject;?> </th>
<th> <?php echo $content;?> </th>
<th> <?php echo $author;?> </th>
<th> <?php echo $email;?> </th>
<th> <?php echo $publish;?></th>
<th> <?php echo $time;?></th>
</tr>
</thead>
<?php
$n = 0;
foreach ($this->messages as &$message):
$checked = JHTML::_('grid.id',$n,$message->message_id);
$subjectLink = JHTML::_('link','index.php?option=com_guestbook&task=edit&cid='.$message->message_id,$message->message_subject);
$contentLink = JHTML::_('link','index.php?option=com_guestbook&task=edit&cid='.$message->message_id,$message->message_content);
?>
<tr class="<?php echo "row".$n%2; ?>">
<td>
<?php echo $checked;?>
</td>
<td>
<?php echo $message->message_id;?>
</td>
<td>
<?php echo $subjectLink;?>
</td>
<td>
<?php echo $contentLink;?>
</td>
<td>
<?php echo $message->message_author;?>
</td>
<td>
<?php echo $message->message_author_email;?>
</td>
<td>
<?php $published = JHTML::_('grid.published',$message->message_publish,$n);?>
<?php echo $published;?>
</td>
<td>
<?php echo $message->message_time;?>
</td>
</tr>
<?php $n++;?>
<?php endforeach;?>
<tfoot>
<td colspan="8">
<?php echo $this->pagination->getListFooter();?>
</td>
</tfoot>
</table>
</div>
<input type="hidden" name="option" value="com_guestbook"/>
<input type="hidden" name="task" value=""/>
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="filter_order" value="<?php echo $listOrder;?>" />
<input type="hidden" name="filter_order_Dir" value="<?php echo $listDirn;?>" />
</form>
上面大代码和以前的代码只修改了三个地方:
1,在开始不为增加了两行代码:
$listOrder = $this->sortColumn;
$listDirn = $this->sortDirection;
这两行代码的意思是从视图类得到要排序的栏 和要排序的方向。
2,将原先直接输出 标题 改为 输出 由JHtml::_()产生的元素。如:$id = JHtml::_('grid.sort','序号','message_id',$listDirn,$listOrder);
这样就可以让标题支持排序了。
3,在最后还增加了两个隐藏的字段 $listOrder ,$listDirn.
这样就能够让系统每次都能过获得当前的排序的字段和方法。
修改了视图模版之后我们还需要修改模型。因为我们需要对数据库获取的记录进行排序。
修改administrator\componets\com_guestbook\models\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(); // 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')); //增加排序 if($this->getState('filter_order')) { // $query->order($db->getEscaped($this->getState('filter_order','message_id')).' '.$db->getEscaped($this->getState('filter_order_Dir'),'ASC')); $query->order($db->getEscaped($this->getState('filter_order', 'message_id')) . ' ' . $db->getEscaped($this->getState('filter_order_Dir', 'ASC'))); } 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); if(!$db->query()) { return false; } return true; } /** * Add sorttable columns to a table **/ public function populateState() { $filter_order = JRequest::getVar('filter_order'); $filter_order_Dir = JRequest::getVar('filter_order_Dir'); $this->setState('filter_order',$filter_order); $this->setState('filter_order_Dir',$filter_order_Dir); } }
我们对模型类的修改也不是很多。只修改了两个地方:
1,修改了getListQuery()方法。
增加了代码:
if($this->getState('filter_order'))
{
$query->order($db->getEscaped($this->getState('filter_order', 'message_id')) . ' ' . $db->getEscaped($this->getState('filter_order_Dir', 'ASC')));
}
这段代码所做的工作就是依据我们传递进去的filter_order和filter_order_Dir来获取数据。默认是按照message_id来进行从小大到排列。
代码写到这个地方,就会对joomla中实现排序的思路非常清新了,我们要做的就是保证传递正确的filter_order(需要排序的字段)和filter_order_Dir(需要排序的顺序)
2,增加了一个方法
public function populateState() { $filter_order = JRequest::getVar('filter_order'); $filter_order_Dir = JRequest::getVar('filter_order_Dir'); $this->setState('filter_order',$filter_order); $this->setState('filter_order_Dir',$filter_order_Dir); }
populate state中文的意思是填充状态。它的工作就是保证我们传递正确的filter_order和filter_order_Dir变量。 poplulateState是JModel类的一个方法,是一个空的方法。我们现在对他进行重载。他所做的事情很简单,获得我们表单提交的 filter_order和filter_order_Dir变量的值,然后后传递给JModel类的。
这样模型类就修改好了。
最后修改一下视图类:
修改administrator\componets\com_guestbook\views\messagelist\view.html.php文件
defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.component.view'); class GuestBookViewMessageList extends JView { // messagelist view display method 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(); $state = $this->get('State'); $this->sortDirection = $state->get('filter_order_Dir'); $this->sortColumn = $state->get('filter_order'); //display the template parent::display($tpl); } /** * 设置工具栏 * * @access protected */ protected function addToolBar() { JToolBarHelper::title('ZMAX留言板'); //JToolBarHelper::addNew该函数接受两个参数,一个是重载的任务 一个是文本 JToolBarHelper::addNew(); JToolBarHelper::editList(); JToolBarHelper::publish(); JToolBarHelper::unpublish(); JToolBarHelper::deleteList("确认删除这条信息?"); } }
改动非常少,增加了3行代码:
$state = $this->get('State');
$this->sortDirection = $state->get('filter_order_Dir');
$this->sortColumn = $state->get('filter_order');
首先得到状态,让后将这些状态赋值。
到此,大功告成!看一下执行的效果:
评论 (0)