教程栏目

joomla中文网出品的官方教程

JModelList类派生自JModel类,这个类比较擅长处理一些列的数据,也就是经常说的结果集。在这个类中实现了操作多数据的一些方法。比喻说分页,排序等等。

使用这个类非常的简单,只需要让我们自己的类继承自JModelList类,然后实现一个getListQuery()方法。一段实例代码如下:

 

<?php

defined('_JEXEC') or die('Restricted Access');

//jimport('joomla.application.component.model');
jimport('joomla.application.component.modellist');

class DownLoadModelItems extends JModelList
{
	
	protected function getListQuery()
	 {
		$db =  JFactory::getDBO();
		
		$query = $db->getQuery(true);
		
		$query->select("*");
		
		$query->from( $db->nameQuote('#__download'));
		
		$query->order($this->getState('list.ordering' ,'id')." ".$this->getState('list.direction' , 'DESC'));
		
		return $query;
		
	 }
	 
}

 

getListQuery就是指明你要操作的表,剩下的事情就交给JModelList类就行了。

 那么你可以在你的视图中写下面的代码了

 function display($tpl = null)	 
	 {
		$items = $this->get('Items');
		$pagination = $this->get('Pagination');
		$state = $this->get('State');
		if(count($error = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />',$errors));
			return false;
		}
		
		$this->items = $items;
		$this->pagination = $pagination;
		
		$this->state = $state;
		$this->addToolBar();
		
		
		
		parent::display($tpl);
		
	 }

 getItems()方法返回结果集。getPagination()方法返回一个分页对象。getState()方法还不是很清楚。这些方法都是JModelList帮实现。几乎上面的代码就是一段标准视图类的代码。

评论 (0)

  • 最新在前
  • 最佳在前

第1章 Joomla入门教程

第3章 C计划

第5章 E计划

第6章 H计划

第7章 G计划

第9章 运行环境

第11章 主从与集群

第12章 模块开发

第13章 插件开发

第14章 j2.x组件开发教程

第15章 页面定制教程

第16章 页面构造器