在前两篇文章:
Joomla!扩展制作实例教程-模板展示组件-后台记录增加和修改程序
Joomla!扩展制作实例教程-模板展示组件-后台增加所见即所得编辑器
我们现在完成了一个基本满意的后台记录修改和添加,删除的部分,但是我们添加几条记录后,就会发现,后台的列表页面,现在每条记录显示了模板的每个字段,内容多了之后,很难看清楚。并且记录多了之后,还可能导致页面的部分脚本出错。现在我们来整理一下后台列表的功能,一是去掉一些不必显示的字段,二是增加分页的功能。
要增加分页功能,涉及到三个文件修改,一是administrator/components/com_showcase/model/showcases.php
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.model' );
class ShowcasesModelShowcases extends JModel
{
var $_data;
function __construct()
{
parent::__construct();
global $mainframe, $option;
// Get the pagination request variables
$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
$limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );
// In case limit has been changed, adjust limitstart accordingly
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
$this->setState('limit', $limit);
$this->setState('limitstart', $limitstart);
}
function _buildQuery()
{
$query = ' SELECT * FROM #__showcase';
return $query;
}
function getData()
{
// Lets load the content if it doesn't already exist
if (empty($this->_data))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
}
return $this->_data;
}
function getTotal()
{
// Lets 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()
{
// Lets 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;
}
}
我们看到,相对原来的文件,增加了三个函数,并对getData函数进行了修改。现在构造函数,取得了后台分页配置的基本信息,每页显示多少记录,以及当前是多少页。而getTotal函数是取得目前数据表中有多少记录。
getPagination函数,怎取得了当前的分页的对象。其实这个文件的所有函数基本在所有的后台列表model的文件都可以通用,你只要修改build query中查询语句就足够了。
接着我们还需要对 administrator/components/com_showcase/views/showcases/view.html.php,这个改动相对较小,只是在display函数增加了两行:
$items = & $this->get( 'Data');
$total = & $this->get( 'Total');
$pagination = & $this->get( 'Pagination' );
$this->assignRef('items', $items);
$this->assignRef('pagination', $pagination);
最后,我们修改一些tmpl/default.php 位于showcases/tmpl目录下,注意啊,是showcases ,少一个s,那就错了。
<?php defined('_JEXEC') or die('Restricted access'); ?>
<form action="index.php" method="post" name="adminForm">
<div id="editcell">
<table>
<?php
$k = 0;
for ($i=0, $n=count( $this->items ); $i < $n; $i++)
{
$row = &$this->items[$i];
$checked = JHTML::_('grid.id', $i, $row->id );
$link = JRoute::_( 'index.php?option=com_showcase&controller=showcase&task=edit&cid[]='. $row->id );
?>
<tr class="<?php echo "row$k"; ?>">
<td>
<?php echo $row->id; ?>
</td>
<td>
<?php echo $checked; ?>
</td>
<td><a href="/<?php echo $link; ?>"><?php echo $row->catid?></a></td>
<td><a href="/<?php echo $link; ?>"><?php echo $row->title?></a></td>
<td><a href="/<?php echo $link; ?>"><?php echo $row->urldownload?></a></td>
<td><a href="/<?php echo $link; ?>"><?php echo $row->downloads?></a></td>
</tr>
<?php
$k = 1 - $k;
}
?>
</table>
</div>
<input type="hidden" name="option" value="com_showcase" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="showcase" />
<?php echo $this->pagination->getListFooter();?>
</form>
可以看到,我们只是减少了显示列数,注意</form>之前这一行,我们显示了分页部分的代码。
经过以上的修改,我们就达到了列表页面的修改目的。附件中是这次修改,涉及到的文件,请参考。
现在,我们的后台列表页面如下:

Attatchments: 您还没有登录,登录后方能下载,如果您还没有注册,请点击
免费注册