`
seaboycs
  • 浏览: 127973 次
  • 性别: Icon_minigender_1
  • 来自: 南通
社区版块
存档分类
最新评论
阅读更多

闲来无事,做了一个多级地址的demo,模仿了京东的样式:

 

采用了spring mvc的框架,下面把代码贴上,以后好找:

 

数据库:

CREATE TABLE IF NOT EXISTS COM_PRO_COUNTRY (	`ID` INT(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR (50) NULL,`FULLNAME` VARCHAR (50) NULL,PRIMARY KEY (`ID`)) COLLATE='utf8_bin' ENGINE=InnoDB AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS COM_PRO_PROVINCE (	`ID` INT(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR (50) NULL,`FULLNAME` VARCHAR (50) NULL,`COUNTRY` INT (50) NULL,PRIMARY KEY (`ID`)) COLLATE='utf8_bin' ENGINE=InnoDB AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS COM_PRO_CITY (	`ID` INT(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR (50) NULL,`FULLNAME` VARCHAR (50) NULL,`PROVINCE` INT (50) NULL,PRIMARY KEY (`ID`)) COLLATE='utf8_bin' ENGINE=InnoDB AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS COM_PRO_DISTRICT (	`ID` INT(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR (50) NULL,`FULLNAME` VARCHAR (50) NULL,`CITY` INT (50) NULL,PRIMARY KEY (`ID`)) COLLATE='utf8_bin' ENGINE=InnoDB AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS COM_PRO_ROAD (	`ID` INT(11) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR (50) NULL,`FULLNAME` VARCHAR (50) NULL,`DISTRICT` INT (50) NULL,PRIMARY KEY (`ID`)) COLLATE='utf8_bin' ENGINE=InnoDB AUTO_INCREMENT=1;

 可以看出来,有5张表:国家,省市,城市,区,路。京东默认是省,市,区。

 

Java 代码,主要功能:查询所有国家,根据国家查询所有省市,根据省市查询所有城市,以此类推。

 

Controller 相关代码:

    @RequestMapping(value = "/preAdd.spring", method = RequestMethod.GET)
    public ModelAndView preAdd() throws Exception{
	ModelAndView mav = new ModelAndView();
	String pageUrl = "admin/address/add";
	// add other logic.
	mav.setViewName(pageUrl);
	mav.addObject("addressBean", new AddressBean());
	mav.addObject("RoadBeanList", roadService.list());
	mav.addObject("CountryBeanList", countryService.list());
	
	return mav;
    }    

    @RequestMapping(value = "/getProvinces.spring", method = RequestMethod.POST)
    public @ResponseBody List<ProvinceBean> getProvinces(@RequestParam(value = "id", required = false) Integer id) throws Exception{
    	return provinceService.list(id);
    }
    
    @RequestMapping(value = "/getCitys.spring", method = RequestMethod.POST)
    public @ResponseBody List<CityBean> getCitys(@RequestParam(value = "id", required = false) Integer id) throws Exception{
    	return cityService.list(id);
    }
    
    @RequestMapping(value = "/getDistricts.spring", method = RequestMethod.POST)
    public @ResponseBody List<DistrictBean> getDistricts(@RequestParam(value = "id", required = false) Integer id) throws Exception{
    	return districtService.list(id);
    }
    
    @RequestMapping(value = "/getRoads.spring", method = RequestMethod.POST)
    public @ResponseBody List<RoadBean> getRoads(@RequestParam(value = "id", required = false) Integer id) throws Exception{
    	return roadService.list(id);
    }

 

Service 的部分代码:

    @Override
    public List<CountryBean> list() throws ProException {
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM COM_PRO_COUNTRY");
        if (!CommonUtils.isEmptyList(rows)) {
            List<CountryBean> beanList = new ArrayList<CountryBean>();
            for (Map<String, Object> row : rows) {
                CountryBean countryBean = new CountryBean();
                countryBean.setId((Integer)row.get("ID"));
                countryBean.setName((String)row.get("NAME"));
                countryBean.setFullName((String)row.get("FULLNAME"));
                beanList.add(countryBean);
            }
            return beanList;
        }
         return null;
    }

    @Override
    public List<ProvinceBean> list(int id) throws ProException {
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM COM_PRO_PROVINCE WHERE COUNTRY = ?", id);
        if (!CommonUtils.isEmptyList(rows)) {
            List<ProvinceBean> beanList = new ArrayList<ProvinceBean>();
            for (Map<String, Object> row : rows) {
                ProvinceBean provinceBean = new ProvinceBean();
                provinceBean.setId((Integer)row.get("ID"));
                provinceBean.setName((String)row.get("NAME"));
                provinceBean.setFullName((String)row.get("FULLNAME"));
                beanList.add(provinceBean);
            }
            return beanList;
        }
         return null;
    }

    @Override
    public List<CityBean> list(int id) throws ProException {
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM COM_PRO_CITY WHERE PROVINCE =?", id);
        if (!CommonUtils.isEmptyList(rows)) {
            List<CityBean> beanList = new ArrayList<CityBean>();
            for (Map<String, Object> row : rows) {
                CityBean cityBean = new CityBean();
                cityBean.setId((Integer)row.get("ID"));
                cityBean.setName((String)row.get("NAME"));
                cityBean.setFullName((String)row.get("FULLNAME"));
                beanList.add(cityBean);
            }
            return beanList;
        }
         return null;
    }

    @Override
    public List<DistrictBean> list(int id) throws ProException {
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM COM_PRO_DISTRICT WHERE CITY=?", id);
        if (!CommonUtils.isEmptyList(rows)) {
            List<DistrictBean> beanList = new ArrayList<DistrictBean>();
            for (Map<String, Object> row : rows) {
                DistrictBean districtBean = new DistrictBean();
                districtBean.setId((Integer)row.get("ID"));
                districtBean.setName((String)row.get("NAME"));
                districtBean.setFullName((String)row.get("FULLNAME"));
                beanList.add(districtBean);
            }
            return beanList;
        }
         return null;
    }

    @Override
    public List<RoadBean> list(int id) throws ProException {
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM COM_PRO_ROAD WHERE DISTRICT=?", id);
        if (!CommonUtils.isEmptyList(rows)) {
            List<RoadBean> beanList = new ArrayList<RoadBean>();
            for (Map<String, Object> row : rows) {
                RoadBean roadBean = new RoadBean();
                roadBean.setId((Integer)row.get("ID"));
                roadBean.setName((String)row.get("NAME"));
                roadBean.setFullName((String)row.get("FULLNAME"));
                beanList.add(roadBean);
            }
            return beanList;
        }
         return null;
    }

 

JSP 相关代码:CSS用到的图片在附件里有.

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>地址</title>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<style type="text/css">
body {
	padding-bottom: 40px;
}

.sidebar-nav {
	padding: 9px 0;
}

.addressDiv {
	display: none;
	position: absolute;
	border: 1px solid #CECBCE;
	width: 390px;
	padding: 15px;
	background: #fff;
	-moz-box-shadow: 0 0 5px #ddd;
	-webkit-box-shadow: 0 0 5px #ddd;
	box-shadow: 0 0 5px #ddd;
	font: 12px/150% Arial, Verdana, "\5b8b\4f53";
}

.stock {
	position: relative;
	margin-bottom: 10px;
}

.stock .mt .tab {
	width: 100%;
	height: 25px;
	float: left;
	border-bottom: 2px solid #edd28b;
	overflow: visible;
	list-style: none;
	margin: 0 0 9px 0px;
}

.stock .mt .tab li {
	margin-left: 5px;
	float: left;
	clear: none;
	padding: 0px 5px 5px;
	border: 2px solid #CECBCE;
	font-style: normal;
	border-bottom-color: #edd28b;
}

.stock .mt .tab .curr {
	border: 2px solid #edd28b;
	border-bottom-color: aliceblue;
}

.stock .mt .tab li a em {
	font-style: normal;
}

.clr {
	display: block;
	overflow: hidden;
	clear: both;
	height: 0;
	line-height: 0;
	font-size: 0;
}

.mt {
	cursor: default;
}

.stock-line {
	margin: 0;
	padding: 0;
}

.m,.mt,.mc,.mb,.sm,.smt,.smc,.smb {
	overflow: hidden;
	zoom: 1;
}

.area-list {
	padding-top: 5px;
	list-style: none;
	float: left;
}

.area-list li {
	padding: 2px 0 2px 15px;
	width: 50px;
	float: left;
	clear: none;
}

.close {
	position: absolute;
	display: inline-block; 
	width : 14px; 
	height : 14px; 
	line-height : 14px;
	vertical-align : text-top; 
	background-image :url("../img/glyphicons-halflings.png");
	background-position: -168px -96px;
	background-repeat: no-repeat;
}
</style>

</head>
<body> 
               <input class="input-xlarge focused" id="addressPath" type="text" onfocus="openAddressDiv(this)" readonly />

		<div id="addressDIV" class="addressDiv">

			<div id="stock" class="stock" data-widget="tabs">
				<div class="mt">
					<ul class="tab">
						<li data-index="0" id="addressTab0" class="curr"
							data-widget="tab-item" class="display: list-item;"
							onclick="changeTab(0)"><a href="#none"><em>${CountryBeanList[0].name}</em><i></i></a></li>
						<li data-index="1" id="addressTab1" data-widget="tab-item"
							style="display: none;" class="" onclick="changeTab(1)"><a
							href="#none" class="" title=""><em></em><i></i></a></li>
						<li data-index="2" id="addressTab2" data-widget="tab-item"
							style="display: none;" class="curr" onclick="changeTab(2)"><a
							href="#none" class="hover" title=""><em></em><i></i></a></li>
						<li data-index="3" id="addressTab3" data-widget="tab-item"
							style="display: none;" onclick="changeTab(3)"><a
							href="#none" class="" title=""><em></em><i></i></a></li>
						<li data-index="4" id="addressTab4" data-widget="tab-item"
							style="display: none;" onclick="changeTab(4)"><a
							href="#none" class="" title=""><em></em><i></i></a></li>
					</ul>
					<div class="stock-line"></div>
				</div>
				<div class="mc" id="areaList0" data-area="0"
					data-widget="tab-content" id="stock_province_item"
					style="display: block;">
					<ul class="area-list">
						<c:forEach var="myItem" items="${CountryBeanList}">
							<li><a href="#none" data-value="${myItem.id}"
								onclick="loadProvince('${myItem.id}', '${myItem.name}')">${myItem.name}</a></li>
						</c:forEach>

					</ul>
				</div>
				<div class="mc" id="areaList1" data-area="1"
					data-widget="tab-content" id="stock_city_item"
					style="display: none;">
					<ul class="area-list">
					</ul>
				</div>
				<div class="mc" id="areaList2" data-area="2"
					data-widget="tab-content" id="stock_area_item"
					style="display: none;">
					<ul class="area-list">

					</ul>
				</div>
				<div class="mc" id="areaList3" data-area="3"
					data-widget="tab-content" id="stock_town_item"
					style="display: none;">
					<ul class="area-list">

					</ul>
				</div>
				<div class="mc" id="areaList4" data-area="4"
					data-widget="tab-content" id="stock_town_item"
					style="display: none;">
					<ul class="area-list">

					</ul>
				</div>
			</div>
			<span class="clr"></span>
			<div id="closeAddressDivImg" class="close" onclick="closeAddressDiv()"></div>
		</div>
<script src='../js/address2.js'></script>
</body>
</html>

 

JS:

var inputElement;
function changeTab(id) {
	clearCSS();
	$("#areaList" + id).css("display", "");
	$("#addressTab" + id).addClass("curr");
}

function loadProvince(id, name) {
	$("#addressTab0 a em").text(name);
	clearCSS();
	$("#addressTab2").css("display", "none");
	$("#addressTab3").css("display", "none");
	$("#addressTab4").css("display", "none");
	loadProvinces(id);
}

function doAjaxCall(url, data, suc) {
	$.ajax({
		url : url,
		type : "POST",
		data : data,
		success : suc,
		error : function(x, e) {
			alert(e);
		}
	});
}

function loadProvinces(id) {
	var data = {
		id : id
	};
	doAjaxCall("/shop/admin/address/getProvinces.spring", data,
			loadProvincesSucc);
}

function loadProvincesSucc(data) {
	loadSucc(data, 'loadCity', 1);
}

function loadSucc(data, loadName, index) {
	var inhtml = "";
	for ( var i = 0; i < data.length; i++) {
		inhtml += "<li><a href='#none' data-value='" + data[i].id
				+ "' onclick=\"" + loadName + "('" + data[i].id + "','"
				+ data[i].name + "')\">" + data[i].name + "</a></li>";
	}
	$("#areaList" + index + " ul").html(inhtml);
	$("#areaList" + index).css("display", "");
	$("#addressTab" + index).css("display", "");
	$("#addressTab" + index).addClass("curr");
	$("#addressTab" + index + " a em").text("请选择");
}

function loadCity(id, name) {
	$("#addressTab1 a em").text(name);
	clearCSS();
	$("#addressTab3").css("display", "none");
	$("#addressTab4").css("display", "none");
	loadCities(id);
}

function loadCities(id) {
	var data = {
		id : id
	};
	doAjaxCall("/shop/admin/address/getCitys.spring", data, loadCitySucc);
}

function loadCitySucc(data) {
	loadSucc(data, 'loadDistrict', 2);
}

function loadDistrict(id, name) {
	$("#addressTab2 a em").text(name);
	clearCSS();
	$("#addressTab4").css("display", "none");
	loadDistricts(id);
}

function loadDistricts(id) {
	var data = {
		id : id
	};
	doAjaxCall("/shop/admin/address/getDistricts.spring", data,
			loadDistrictSucc);
}

function loadDistrictSucc(data) {
	loadSucc(data, 'loadRoad', 3);
}

function loadRoad(id, name) {
	$("#addressTab3 a em").text(name);
	clearCSS();
	loadRoads(id);
}

function loadRoads(id) {
	var data = {
		id : id
	};
	doAjaxCall("/shop/admin/address/getRoads.spring", data, loadRoadSucc);
}

function loadRoadSucc(data) {
	loadSucc(data, 'submitRoad', 4);
}

function submitRoad(id, name) {
	$("#addressTab4 a em").text(name);
	var addressPath = $("#addressTab0 a em").text() + $("#addressTab1 a em").text() + $("#addressTab2 a em").text() + $("#addressTab3 a em").val() + $("#addressTab4 a em").text(); 
	$(inputElement).val(addressPath);
}

function clearCSS() {

	$("#areaList0").css("display", "none");
	$("#areaList1").css("display", "none");
	$("#areaList2").css("display", "none");
	$("#areaList3").css("display", "none");
	$("#areaList4").css("display", "none");

	$("#addressTab0").removeClass("curr");
	$("#addressTab1").removeClass("curr");
	$("#addressTab2").removeClass("curr");
	$("#addressTab3").removeClass("curr");
	$("#addressTab4").removeClass("curr");
}

function openAddressDiv(e) {
	$("#addressDIV").css("left", e.offsetLeft + "px");
	$("#addressDIV").css("top", (e.offsetTop + e.offsetHeight) + "px");
	$("#addressDIV").fadeIn();
	
	$("#closeAddressDivImg").css("left", ($("#addressDIV").get(0).offsetWidth - 8) +"px");
	$("#closeAddressDivImg").css("top",  "-7px");
	
	inputElement = e;
}

function closeAddressDiv() {
	 $("#addressDIV").fadeOut();
}

 

相关架包:由于架包比较多(spring 3.1.0.RELEASE.jar +jackson 1.8.1.jar),就不上传了,谁想要的可以联系我,我发给你。


 

  • 大小: 25 KB
  • 大小: 13.5 KB
分享到:
评论

相关推荐

    ios-仿京东多级选择的pickerView.zip

    在“ios-仿京东多级选择的pickerView.zip”这个项目中,开发者MuYanQin创建了一个模仿京东商城多级筛选功能的自定义PickerView控件,名为MCPickerView。这个组件对于实现商品分类、地区筛选等功能非常实用,特别是在...

    PagerSlidingTabStrip 仿京东所在地区级联选择

    总之,"PagerSlidingTabStrip 仿京东所在地区级联选择"是一个结合了滑动标签和多级选择功能的实例,它展示了Android开发中的数据处理、UI设计和用户体验优化。通过对这个项目的学习,开发者可以提升自己在Android...

    ios-仿京东地址选择器.zip

    2. **多级选择**: 地址选择器通常涉及到省级、市级、区县级等不同层次的地理位置信息。在"仿京东地址选择器"中,它支持3级和4级地址选择,意味着用户可以选择省、市、区/县,甚至街道等更细化的级别。 3. **本地...

    JDSelector, 仿京东的地址选择做的一个自定义深度的多级选择器.zip

    《JDSelector:一款仿京东地址选择的自定义深度多级选择器详解》 在移动应用开发中,用户界面的设计和交互体验至关重要。特别是在涉及到地域选择时,如何提供一个直观、易用且高效的多级选择器成为了开发者面临的一...

    安卓三级联动省市联动多级联动相关-仿京东商城选择地区样式Demo.rar

    在Android开发中,"三级联动省市联动多级联动"是一种常见的功能需求,特别是在电商应用中,用户需要选择收货地址时。此功能涉及到省、市、区(县)的逐级选择,每级的选择结果会影响到下一级的选择项。在本案例中,...

    swift-hdselecter仿京东地址选择

    对于这个项目,我们可能会使用这些控件来构建一个多级选择的界面。每个级别(省、市、区县)都可能是一个独立的列表,通过点击上一级别的选项来加载下一级别的数据。 1. **Swift基础知识**:首先,理解Swift的基本...

    安卓三级联动省市联动多级联动相关-仿京东城市代码本地json数据可以获取城市代码和城市名字超简单易懂.rar

    在Android开发中,"三级联动省市联动多级联动"是一个常见的需求,特别是在电子商务、物流配送等领域,用户需要选择他们的收货地址,这通常涉及到国家、省份、城市等多层次的选择。这个压缩包文件提供了实现这一功能...

    Android-一款通用的筛选组件支持单选复选多级筛选筛选联动筛选数据懒加载

    "Android-一款通用的筛选组件支持单选复选多级筛选筛选联动筛选数据懒加载"这个项目,提供了一个功能强大的筛选组件,适用于各种应用场景。下面将详细阐述其主要特性。 1. **单选与复选**: 这个筛选组件支持单选...

    仿京东商城Android客户端底部菜单源码

    这种设计提高了用户体验,因为用户无需深入多级菜单就能访问核心功能。 在该源码中,我们可以学习如何实现以下几点: 1. **自定义布局**:源码可能包含了自定义的布局文件,用于创建底部导航栏,包括图标和文字的...

    address_pickerview-master.zip

    2. **RecyclerView/ListView**:为了展示层级结构,开发者可能使用了RecyclerView或ListView,它们是Android中用于显示可滚动列表的控件,可以很好地适应动态数据。 3. **Adapter模式**:适配器模式在处理列表数据...

    AddressDemo.zip

    例如,添加自定义的地址项或支持多级定制。 7. **代码组织**:为了保持代码的清晰和可读性,通常我们会将相关的代码封装到类别、分类或单独的视图控制器中。此外,遵循MVC(Model-View-Controller)设计模式可以...

Global site tag (gtag.js) - Google Analytics