日期:2014-05-16  浏览次数:20795 次

ext中ComboBox+Ajax+分页联动代码和注释

今天花了1下午时间研究出了ComboBox+Ajax+分页联动效果.ext学习初步见效

下面代码直接就能运行,用list模拟了数据库中取数据

以下是目录结构



?以下是代码

test1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>测试1</title>
    <link rel="stylesheet" href="../extjs/resources/css/ext-all.css" type="text/css"></link>
	<link rel="stylesheet" type="text/css" href="../styles.css">
	<script type="text/javascript" src="../extjs/ext-base.js"></script>
	<script type="text/javascript" src="../extjs/ext-all.js"></script>
	<script type="text/javascript" src="../extjs/ext-lang-zh_CN.js"></script>
	<script type="text/javascript" src="test1/test1.js" charset="utf-8"></script>
  </head>
  
  <body>
  </body>
</html>

?

?test1.js

Ext.onReady(function(){
	
	//设置省份数据源storeProvinces
	var storeProvinces = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: "../ProvincesJsonServlet"}),
		reader: new Ext.data.JsonReader({}, [
			{name: "provincesId", type: "int", mapping: "provincesId"},
			{name: "provincesName", type: "string", mapping: "provincesName"}
		])
	});
	
	//创建组件comboProvinces,省份下拉框
	var comboProvinces = new Ext.form.ComboBox({
		renderTo: Ext.getBody(),
		triggerAction: "all",
		store: storeProvinces,
		displayField: "provincesName",
		valueField: "provincesId",
		mode: "remote",
		readOnly: true,
		emptyText: "请选择省份",
		listeners:{
		//监听下拉选择事件
                  		select:function(combo,record,index){
                  		 //设置数据源
        				 storeCities.proxy= new Ext.data.HttpProxy({url: "../CitiesJsonServlet?provincesId="+comboProvinces.getValue()});
        				 //传参start,limit
        				 storeCities.load({params:{start:0,limit:5}});
        				 //清空comboCities
        				 comboCities.clearValue();
                  		}
        		}
	});
	
	//设置城市数据源storeCities
	var storeCities = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy(),
		reader: new Ext.data.JsonReader({
			totalProperty: "totalProperty",		//总记录数
			root: "root"	//所有的数据(json对象数组)
		}, [
			{name: "cityId", type: "int", mapping: "cityId"},	//设置名称、类型、映射关系
			{name: "cityName", type: "string", mapping: "cityName"}
		])
	});
	
	//创建组件comboCities,城市下拉框
	var comboCities = new Ext.form.ComboBox({
            		store: storeCities,			//数据源
            	emptyText:'请选择地区',		//空值时显示
            	mode: 'remote',			//远程数据
            	pageSize: 5,			//分页大小
            	triggerAction: 'all',
            		displayField: "cityName",		//显示值
            	valueField :"cityId",			//实际值
            	renderTo: Ext.getBody(),
            		readOnly: true,			//只读
            	listWidth: 300			//下拉宽度
	});
});

??web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ProvincesJsonServlet</servlet-name>
    <servlet-class>ajax.ProvincesJsonServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>CitiesJsonServlet</servlet-name>
    <servlet-class>ajax.CitiesJsonServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ProvincesJsonServlet</servlet-name>
    <url-pattern>/ProvincesJsonServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    &l