省市区3级级联checkTree

本例是一次性加载完成的,适用于数据量不是很大的,不能用ajax一个节点一个节点来加载的例子

比如我下方的省市区3级加载,总共不到4000节点
本文是在下面这个控件的基础上完成的。

http://www.iteye.com/topic/487771

效果如下:

编辑页面:

编码部分:

js:

function selectRegion(pathContext,type,slctrId,checkedId){
	if(typeof(slctrId) == "undefined"){
		slctrId = "";
	}
	if(typeof(checkedId) == "undefined"){
		checkedId = "";
	}
	var params = new Map();
	params.put("type",type);
	params.put("slctrId",slctrId);
	params.put("checkedId",checkedId);	
	var returnValue = showModalDlg(pathContext + 'pages/mn/jsTree/jsonTree.jsp', params, '300', '600');
	if(returnValue==""||returnValue==undefined){
	}else{
		var res = returnValue.split("_");
		document.getElementById("slctArea").value = res[1];
		document.getElementById("slctAreaId").value = res[0];
	}
}

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>管理地区选择</title>
<%@ include file="../html_header.jsp"%>
<link rel="stylesheet" type="text/css"
	href="${PATH_CONTEXT}pages/mn/jsTree/TreePanel.css">
<script type=text/javascript
	src="${PATH_CONTEXT}pages/mn/jsTree/common-min.js"></script>
<script type=text/javascript
	src="${PATH_CONTEXT}pages/mn/jsTree/TreePanel.js"></script>
<script type="text/javascript"> 
	var tree = null;
	
	var url = PATH_CONTEXT + "MN/TreeAction.do";
	
	var params = new Map();
	
	params = window.dialogArguments;
	
	requestAction(url, regionCallback, params);
	
	function regionCallback(params){
		var json = params.get("json");
		var res = eval('{' + json + '}');//将string类型转换为json使用的object类型
		tree = new TreePanel({
			'root' : res[0].root[0],
			'renderTo' : divTree
		});
		tree.render();
	}
	
	Array.prototype.del=function(n) { 
	//n表示第几项,从0开始算起。
	//prototype为对象原型,注意这里为对象增加自定义方法的方法。
	 if(n<0) //如果n<0,则不进行任何操作。
	  return this;
	 else
	  return this.slice(0,n).concat(this.slice(n+1,this.length));
	  /*
	   concat方法:返回一个新数组,这个新数组是由两个或更多数组组合而成的。
	         这里就是返回this.slice(0,n)/this.slice(n+1,this.length)
	         组成的新数组,这中间,刚好少了第n项。
	   slice方法: 返回一个数组的一段,两个参数,分别指定开始和结束的位置。
	  */
	}

	function fChecked(){
		if(tree == null){
			alert("管理地区加载中,请稍后...");
		}else{
			var checkId = tree.getChecked();
			var checkName = tree.getCheckedName();
			var checkedPro = [];
			var checkedProName = [];
			var checkedCity = [];
			var checkedCityName = [];
			for(var i = 0 ; i < checkId.length ; i++){
				if(checkId[i] == "000000"){
					checkId = checkId.del(i);//在数组里删除所有中国ID
					checkName = checkName.del(i);
					i--;
				}else{
					if(checkId[i].substring(2,6)=="0000"){
						checkedPro.push(checkId[i]);
						checkedProName.push(checkName[i]);
						checkId = checkId.del(i);//在数组里删除所有省份ID
						checkName = checkName.del(i);
						i--;
					}
				}
			}
			for(var i = 0 ; i < checkedPro.length ; i++){
				for(var j = 0 ; j < checkId.length ; j++){
					if(checkedPro[i].substring(0,2)==checkId[j].substring(0,2)){
						checkId = checkId.del(j);
						checkName = checkName.del(j);
						j--;
					}
				}
			}
			for(var i = 0 ; i < checkId.length ; i++){
				if(checkId[i].substring(4,6)=="00"){
					checkedCity.push(checkId[i]);
					checkedCityName.push(checkName[i]);
					checkId = checkId.del(i);//在数组里删除所有市ID
					checkName = checkName.del(i);
					i--;
				}
			}
			for(var i = 0 ; i < checkedCity.length ; i++){
				for(var j = 0 ; j < checkId.length ; j++){
					if(checkedCity[i].substring(0,4)==checkId[j].substring(0,4)){
						checkId = checkId.del(j);
						checkName = checkName.del(j);
						j--;
					}
				}
			}
			var resId = checkedPro.concat(checkedCity).concat(checkId);
			var resName =  checkedProName.concat(checkedCityName).concat(checkName);
			window.returnValue = resId+"_"+resName;
			window.close();
		}
	}
</script>
</head>
<body>
<form action="javascript:void(0)">
<table width="96%" border="0" cellpadding="0" cellspacing="0"
	bgcolor="#FFFFFF" class="mytab_b" style="height:100%">
	<tr>
		<td height="18px" colspan="4" background="../../images/bg.gif"
			bgcolor="#FFFFFF" class="STYLE3">
		<div align="center">管理地区</div>
		</td>
	</tr>
	<tr>
		<td style="width: 100%" valign="top">
		<div id="divTree"
			style="width:285px;height:500px;overflow:scroll;overflow-x:hidden;">管理地区加载中...</div>
		<table width="100%">
			<tr style="height: 35px">
				<td colspan="5" align="center" style=""><input type="button"
					class="btn3_mouseup" value="确定" onClick="fChecked();" /> <input
					type="button" class="btn3_mouseup" value="取消"
					onClick="window.close();" /></td>
			</tr>
		</table>
		</td>
	</tr>
</table>


TreeAction:

/*
 * Copyright (C) 2011 Founder All Rights Reserved.
 * 
 * XY0401DelAction.java
 */
package cn.founder.web.action.mn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

/**
 * [类名]<br>
 * TreeAction <br>
 * <br>
 * [功能概要]<br>
 * 地区选择器checkTree<br>
 * Tree Author: Relucent(javaEye)
 * <br>
 * [变更历史]<br>
 * 2011/06/20 ver1.00 新規作成 <br>
 * 
 * @author  * @version 1.00
 */
public class TreeAction extends BaseAjaxAction {

	protected SortMap<String, String> doAjaxExecute(SysForm form, HttpServletRequest request,
			HttpServletResponse response) throws DBException, SystemException, WarningException {
		
		long startTime=System.currentTimeMillis();   //获取开始时间

		Map<String,List<Tree>> map = new HashMap<String,List<Tree>>();//树的总map
		
		RegionSelectBiz regionSelectBiz = (RegionSelectBiz) WebBizFactory.getBizInstance(
				RegionSelectBiz.class, request);
		List<Tree> countryTree = new ArrayList<Tree>();//中国
		
		List<Tree> provinceTreeList = null;//省
		
		List<Tree> cityTreeList = null;//市
		
		List<Tree> countryTreeList = null;//区
		
		String slctrId = form.getString("slctrId");
		
		String checkedId = form.getString("checkedId");
		
		String type = form.getString("type");
		

		List<Map<String,Object>> proList = null;
		List<Map<String, Object>> cityList = null;
		List<Map<String, Object>> countyList = null;
		Map<String,List<Map<String, Object>>> cityMap = null;
		Map<String,List<Map<String, Object>>> countyMap = null;
		if(type.equals("1")){
			proList = regionSelectBiz.getRegionList("0");
			cityMap = regionSelectBiz.getRegionMap("1");
			countyMap = regionSelectBiz.getRegionMap("2");
			checkedId = regionSelectBiz.getAllRegionListById(slctrId);
		}else{
			Map<String, List<Map<String, Object>>> map1 = regionSelectBiz.getRegionList1(slctrId);
			proList = map1.get("0");
			cityList = map1.get("1");
			countyList = map1.get("2");
		}
		
		provinceTreeList = new ArrayList<Tree>();

		Tree country = new Tree();
		country.setText("请选择管理地区");
		country.setChecked("0");
		country.setId("000000");

		for(int i=0;i<proList.size();i++){
			Tree proTree = new Tree();
			String proId = proList.get(i).get("REGION_CD").toString();
			String proName = proList.get(i).get("REGION_NM").toString();
			if(checkedId.indexOf(proId)>=0){
				proTree.setChecked("1");
			}else{
				proTree.setChecked("0");
			}
			proTree.setId(proId);
			proTree.setText(proName);
			cityTreeList = new ArrayList<Tree>();
			if(type.equals("1")){
				cityList = cityMap.get(proId);
			}
			if(cityList!=null){
				for(int j=0;j<cityList.size();j++){
					String cityId = cityList.get(j).get("REGION_CD").toString();
					String cityName = cityList.get(j).get("REGION_NM").toString();
					String cityPId = cityList.get(j).get("P_REGION_CD").toString();
					Tree cityTree = new Tree();
					cityTree.setId(cityId);
					cityTree.setText(cityName);
					if(checkedId.indexOf(cityId)>=0){
						cityTree.setChecked("1");
						if(proId.equals(cityPId)&&"0".equals(proTree.getChecked())){
							proTree.setChecked("2");
						}
					}else{
						cityTree.setChecked("0");
					}
					if(cityPId.equals(proId)){
						countryTreeList = new ArrayList<Tree>();
						if(type.equals("1")){
							countyList = countyMap.get(cityId);
						}
						if(countyList!=null){
							for(int k=0;k<countyList.size();k++){
								String countyId = countyList.get(k).get("REGION_CD").toString();
								String countyName = countyList.get(k).get("REGION_NM").toString();
								String countyPId = countyList.get(k).get("P_REGION_CD").toString();
								Tree countyTree = new Tree();
								countyTree.setId(countyId);
								countyTree.setText(countyName);
								if(checkedId.indexOf(countyId)>=0){
									countyTree.setChecked("1");
									if(cityId.equals(countyPId)&&"0".equals(cityTree.getChecked())){
										cityTree.setChecked("2");
									}
									if(countyPId.substring(0,2).equals(proTree.getId().substring(0,2))&&"0".equals(proTree.getChecked())){
										proTree.setChecked("2");
									}
								}else{
									countyTree.setChecked("0");
								}
								if(countyPId.equals(cityId)){
									countryTreeList.add(countyTree);
								}
							}
						}
						cityTree.setChildren(countryTreeList);
						cityTreeList.add(cityTree);
					}
				}
			}
			proTree.setChildren(cityTreeList);
			provinceTreeList.add(proTree);
		}
		// 编辑树的需求
		if(type.equals("1")){
			country.setChildren(provinceTreeList);
		}else{
			if(proList.size()==0){
				if(cityList.size()!=0){
					cityTreeList = new ArrayList<Tree>();
					for(int j=0;j<cityList.size();j++){
						String cityId = cityList.get(j).get("REGION_CD").toString();
						String cityName = cityList.get(j).get("REGION_NM").toString();
						Tree cityTree = new Tree();
						if(checkedId.indexOf(cityId)>=0){
							cityTree.setChecked("1");
						}else{
							cityTree.setChecked("0");
						}
						cityTree.setId(cityId);
						cityTree.setText(cityName);
						countryTreeList = new ArrayList<Tree>();
						for(int k=0;k<countyList.size();k++){
							String countyId = countyList.get(k).get("REGION_CD").toString();
							String countyName = countyList.get(k).get("REGION_NM").toString();
							String countyPId = countyList.get(k).get("P_REGION_CD").toString();
							if(countyPId.equals(cityId)){
								Tree countyTree = new Tree();
								if(checkedId.indexOf(countyId)>=0){
									countyTree.setChecked("1");
									if(cityId.equals(countyPId)&&cityTree.getChecked().equals("0")){
										cityTree.setChecked("2");
									}
								}else{
									countyTree.setChecked("0");
								}
								countyTree.setId(countyId);
								countyTree.setText(countyName);
								countryTreeList.add(countyTree);
							}
						}
						cityTree.setChildren(countryTreeList);
						cityTreeList.add(cityTree);
					}
					country.setChildren(cityTreeList);
				}else{
					countryTreeList = new ArrayList<Tree>();
					for(int k=0;k<countyList.size();k++){
						String countyId = countyList.get(k).get("REGION_CD").toString();
						String countyName = countyList.get(k).get("REGION_NM").toString();
						Tree countyTree = new Tree();
						countyTree.setChecked("0");
						countyTree.setId(countyId);
						countyTree.setText(countyName);
						countryTreeList.add(countyTree);
					}
					country.setChildren(countryTreeList);
				}
			}else{
				country.setChildren(provinceTreeList);
			}
			
		}
		countryTree.add(country);
		map.put("root",countryTree);
		JSONArray jsonObject = JSONArray.fromObject(map);      
		String json = jsonObject.toString(); 
		
		
		SortMap<String, String> params = new SortMap<String, String>();
		
		params.put("json", json);

		long endTime=System.currentTimeMillis(); //获取结束时间
		
		System.out.println("程序运行时间: "+(double)(endTime-startTime)/1000+"s");

		return params;
	}
}


RegionSelectBiz:

/*
 * Copyright (C) 2011 Founder All Rights Reserved.
 * 
 * TxtBkItemBiz.java
 */
package cn.founder.common.biz.xy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * [类名]<br>
 * RegionSelectBiz<br>
 * <br>
 * [功能概要]<br>
 * 地区选择器处理Bean.<br>
 * <br>
 * [变更历史]<br>
 * 2011/05/18 ver1.00 新規作成 <br>
 * 
 * @author  * @version 1.00
 */
public class RegionSelectBiz extends BaseBiz {

	/***
	 * 根据lvl的级别查出它的子区划
	 * @param lvl
	 * 		级
	 * @param pRegionId
	 * 		父编号
	 * @return
	 * @throws DBException
	 * @throws SystemException
	 */
	public List<Map<String, Object>> getAllProvinceList(String lvl,String pRegionId)
			throws DBException, SystemException {
		// 获取地区数据库操作Dao实例
		RegionSelectDao regionSelectDao = (RegionSelectDao) DaoFactory.getDaoInstance(
				RegionSelectDao.class, this);
		
		// 取得地区列表
		return regionSelectDao.srchAllProList(lvl,pRegionId);
	}

	/***
	 * 根据lvl的级别查出它的子区划List
	 * @param lvl
	 * 		级
	 * @return
	 * @throws DBException
	 * @throws SystemException
	 */
	public List<Map<String, Object>> getRegionList(String lvl) throws SystemException, DBException {
		// 获取地区数据库操作Dao实例
		RegionSelectDao regionSelectDao = (RegionSelectDao) DaoFactory.getDaoInstance(
				RegionSelectDao.class, this);
		
		// 取得地区列表
		return regionSelectDao.getRegionList(lvl);
	}
	/***
	 * 根据lvl的级别查出它的子区划Map<上级区划ID,下级区划list>
	 * @param lvl
	 * 		级
	 * @return
	 * @throws DBException
	 * @throws SystemException
	 */
	public Map<String,List<Map<String, Object>>> getRegionMap(String lvl) throws SystemException, DBException {
		// 获取地区数据库操作Dao实例
		RegionSelectDao regionSelectDao = (RegionSelectDao) DaoFactory.getDaoInstance(
				RegionSelectDao.class, this);
		
		// 取得地区列表
		return regionSelectDao.getRegionMap(lvl);
	}

	/***
	 * 根据教材委员会ID查出所有省 市 区 的集合
	 * @param slctrId
	 * 		教材委员会ID
	 * @return
	 * @throws DBException
	 * @throws SystemException
	 */
	public Map<String, List<Map<String, Object>>> getRegionList1(String slctrId) throws DBException, SystemException {
		// 获取地区数据库操作Dao实例
		RegionSelectDao regionSelectDao = (RegionSelectDao) DaoFactory.getDaoInstance(
				RegionSelectDao.class, this);
		
		// 取得地区列表
		return regionSelectDao.getRegionList1(slctrId);
		
	}

	/***
	 * 根据教材委员会ID查出所有选中的省 市 区 的字符串
	 * @param slctrId
	 * 		教材委员会ID
	 * @return
	 * @throws DBException
	 * @throws SystemException
	 */
	public String getAllRegionListById(String slctrId) throws DBException, SystemException {
		// 获取地区数据库操作Dao实例
		RegionSelectDao regionSelectDao = (RegionSelectDao) DaoFactory.getDaoInstance(
				RegionSelectDao.class, this);
		
		// 取得地区列表
		return regionSelectDao.getAllRegionListById(slctrId);
	}

	
}


RegionSelectDao:

/*
 * Copyright (C) 2011 Founder All Rights Reserved.
 * 
 * SchlDao.java
 */
package cn.founder.common.dao.xy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;


/**
 * [类名]<br>
 * RegionSelectDao <br>
 * <br>
 * [功能概要]<br>
 * 地区选择相关数据库操作.<br>
 * <br>
 * [变更历史]<br>
 * 2011/06/08 ver1.00 新規作成 <br>
 * 
 * @author 
 * @version 1.00
 */
public class RegionSelectDao extends BaseDao {

	/**
	 * 根据级别和父类ID取得省(直辖市)/市(直辖市所属区)/区(县) 列表
	 * 
	 * @param lvl
	 *            0:省(直辖市) /1 :市(区)/1:市(直辖市所属区)/2:区
	 * @param pRegionId
	 *            父编号
	 * @return 省(直辖市)/市(直辖市所属区)/区(县)情报列表
	 * @throws DBException
	 *             DB异常
	 */
	public List<Map<String, Object>> srchAllProList(String lvl,String pRegionId)
			throws DBException {

		List<Map<String, Object>> rtnList = null;
		// 获取查询DA
		String sqlId = null;
		if("0".equals(lvl)){
			sqlId = "RegionSelectDao.srchPro";
		}else{
			sqlId = "RegionSelectDao.srchCity";
		}
		DataQuery da = DaUtil.getQueryAccessor(sqlId);
		//设置参数 父地区编号
		da.setParameter(":pRegionlvl", lvl);
		if(!"0".equals(lvl)){
			da.setParameter(":pRegionId", pRegionId);
		}
		// 实行
		rtnList = da.executeQueryAsListEx();
		return rtnList;
	}

	public List<Map<String, Object>> getRegionList(String lvl) throws DBException {
		List<Map<String, Object>> rtnList = null;
		// 获取查询DA
		DataQuery da = DaUtil.getQueryAccessor("RegionSelectDao.srchAll");
		//设置参数 父地区编号
		da.setParameter(":lvl", lvl);
		// 实行
		rtnList = da.executeQueryAsListEx();
		return rtnList;
	}

	public Map<String, List<Map<String, Object>>> getRegionList1(String slctrId) throws DBException {
		List<Map<String, Object>> rtnList = null;
		// 获取查询DA
		DataQuery da = DaUtil.getQueryAccessor("BkSlcTrDao.getRegionList1");
		//设置参数 父地区编号
		da.setParameter(":slctrId", slctrId);
		// 实行
		rtnList = da.executeQueryAsListEx();
		Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
		Map<String, Object> map1 = rtnList.get(0);;
		List<Map<String, Object>> proList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> cityList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> countyList = new ArrayList<Map<String, Object>>();
		String[] regionNm = map1.get("REGION_NM").toString().split(",");
		String[] regionCd = map1.get("REGION_CD").toString().split(",");
		String[] regionPcd = map1.get("P_REGION_CD").toString().split(",");
		String[] regionLvl = map1.get("REGION_LVL").toString().split(",");
		Map<String, Object> m = null;
		for(int i=0;i<regionCd.length;i++){
			m = new HashMap<String, Object>();
			m.put("REGION_NM",regionNm[i]);
			m.put("REGION_CD",regionCd[i]);
			m.put("P_REGION_CD",regionPcd[i]);
			m.put("REGION_LVL",regionLvl[i]);
			if("0".equals(regionLvl[i])){
				proList.add(m);
			}
			if("1".equals(regionLvl[i])){
				cityList.add(m);
			}
			if("2".equals(regionLvl[i])){
				countyList.add(m);
			}
		}
		if(proList.size()!=0){
			//取出所有的市、区
			for(int i=0;i<proList.size();i++){
				da = DaUtil.getQueryAccessor("RegionSelectDao.getCountyByProId");
				Object proId = proList.get(i).get("REGION_CD");
				da.setParameter(":proId", proId);
				rtnList = da.executeQueryAsListEx();
				if(rtnList.size()!=0){
					for(int j=0;j<rtnList.size();j++){
						m = new HashMap<String, Object>();
						m.put("REGION_NM",rtnList.get(j).get("Q_REGION_NM"));
						m.put("REGION_CD",rtnList.get(j).get("Q_REGION_CD"));
						m.put("P_REGION_CD",rtnList.get(j).get("C_REGION_CD"));
						countyList.add(m);
					}
				}
				da = DaUtil.getQueryAccessor("RegionSelectDao.getCityByProId");
				da.setParameter(":proId", proId);
				rtnList = da.executeQueryAsListEx();
				if(rtnList.size()!=0){
					for(int j=0;j<rtnList.size();j++){
						m = new HashMap<String, Object>();
						m.put("REGION_NM",rtnList.get(j).get("REGION_NM"));
						m.put("REGION_CD",rtnList.get(j).get("REGION_CD"));
						m.put("P_REGION_CD",proId);
						cityList.add(m);
					}
				}
			}
		}else{
			if(cityList.size()!=0){
				for(int i=0;i<cityList.size();i++){
					da = DaUtil.getQueryAccessor("RegionSelectDao.getCountyByCityId");
					Object cityId = cityList.get(i).get("REGION_CD");
					da.setParameter(":cityId", cityId);
					rtnList = da.executeQueryAsListEx();
					if(rtnList.size()!=0){
						for(int j=0;j<rtnList.size();j++){
							m = new HashMap<String, Object>();
							m.put("REGION_NM",rtnList.get(j).get("Q_REGION_NM"));
							m.put("REGION_CD",rtnList.get(j).get("Q_REGION_CD"));
							m.put("P_REGION_CD",cityId);
							countyList.add(m);
						}
					}
				}
			}
		}
		map.put("0",proList);
		map.put("1",cityList);
		map.put("2",countyList);
		return map;
	}

	public String getAllRegionListById(String slctrId) throws DBException {
		String result = "";
		if(slctrId != null && !"".equals(slctrId)){
			List<Map<String, Object>> rtnList = null;
			// 获取查询DA
			DataQuery da = DaUtil.getQueryAccessor("RegionSelectDao.getAllRegionListById");
			//设置参数 父地区编号
			da.setParameter(":slctrId", slctrId);
			// 得到委员会所管理的地区
			rtnList = da.executeQueryAsListEx();
			//下一步将regionId转换为所有区划
			if(rtnList.size()!=0){
				Set<String> set = new HashSet<String>();
				String regionId = rtnList.get(0).get("REGION_CD")+"";
				String[] regionIds = regionId.split(",");
				List<String> regionIdsPro = new ArrayList<String>();;
				List<String> regionIdsCity = new ArrayList<String>();;
				List<String> regionIdsCounty = new ArrayList<String>();;
				for(int i=0;i<regionIds.length;i++){
					if(regionIds[i].substring(2,6).equals("0000")){
						regionIdsPro.add(regionIds[i]);
					}else if(regionIds[i].substring(4,6).equals("00")){
						regionIdsCity.add(regionIds[i]);
					}else{
						regionIdsCounty.add(regionIds[i]);
					}
				}
				if(regionIdsPro.size()!=0){
					List<Map<String, Object>> rtnList1 = null;
					//当ID为省的时候取出所有的区,市,省ID的集合
					for(int i=0;i<regionIdsPro.size();i++){
						String proId = regionIdsPro.get(i);
						set.add(proId);
						da = DaUtil.getQueryAccessor("RegionSelectDao.getCountyByProId");
						da.setParameter(":proId", proId);
						rtnList1 = da.executeQueryAsListEx();
						if(rtnList1.size()!=0){
							for(int j=0;j<rtnList1.size();j++){
								set.add(rtnList1.get(j).get("Q_REGION_CD")+"");
							}
						}
						da = DaUtil.getQueryAccessor("RegionSelectDao.getCityByProId");
						da.setParameter(":proId", proId);
						rtnList1 = da.executeQueryAsListEx();
						if(rtnList1.size()!=0){
							for(int j=0;j<rtnList1.size();j++){
								set.add(rtnList1.get(j).get("REGION_CD")+"");
							}
						}
					}
				}
				if(regionIdsCity.size()!=0){
					//当ID为市的时候取出所有的区,市ID的集合
					List<Map<String, Object>> rtnList1 = null;
					for(int i=0;i<regionIdsCity.size();i++){
						String cityId = regionIdsCity.get(i);
						set.add(cityId);
						da = DaUtil.getQueryAccessor("RegionSelectDao.getCountyByCityId");
						da.setParameter(":cityId", cityId);
						rtnList1 = da.executeQueryAsListEx();
						if(rtnList1.size()!=0){
							for(int j=0;j<rtnList1.size();j++){
								set.add(rtnList1.get(j).get("Q_REGION_CD")+"");
							}
						}
					}
				}
				for(int j=0;j<regionIdsCounty.size();j++){
					set.add(regionIdsCounty.get(j)+"");
				}
				StringBuffer sb = new StringBuffer();
				Object[] o = set.toArray();
				for(int j=0;j<o.length;j++){
					sb.append(o[j]);
					if(j!=o.length-1){
						sb.append(",");
					}
				}
				return sb.toString();
			}
		}
		return result;
	}

	/***
	 * 根据lvl的级别查出它的子区划Map<上级区划ID,下级区划list>
	 * @param lvl
	 * 		级
	 * @return
	 * @throws DBException
	 */
	public Map<String, List<Map<String, Object>>> getRegionMap(String lvl) throws DBException {
		List<Map<String, Object>> rtnList = null;
		// 获取查询DA
		DataQuery da = DaUtil.getQueryAccessor("RegionSelectDao.srchAll");
		//设置参数 父地区编号
		da.setParameter(":lvl", lvl);
		// 实行
		rtnList = da.executeQueryAsListEx();
		Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
		List<Map<String, Object>> resList = null;
		for(int i=0;i<rtnList.size();i++){
			String pId = rtnList.get(i).get("P_REGION_CD").toString();//上级区划ID
			if(!map.containsKey(pId)){
				resList = new ArrayList<Map<String, Object>>();
				resList.add(rtnList.get(i));
				map.put(pId,resList);
			}else{
				resList = map.get(pId);
				resList.add(rtnList.get(i));
				map.put(pId,resList);
			}
		}
		return map;
	}

}



 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值