先聲明, 所有素材是從ydUI扒下來的, 抄襲可恥,尊重原著 這裏放 官網
點擊 四級地址演示: 四級地址演示 http://www.jq22.com/yanshi18668
三級地址演示 http://www.ydui.org/cityselect
-----------------我這裏只有四級地址的, 三級地址的需要自己去分析JSON數據,然後-----------------------------------
開始:
這是需要的JS文件 這裏直接F12 這個地址
http://www.jq22.com/yanshi18668
將上述JS和 CSS文件複製到你的項目中
還有一個jquery.min.js 自行下載 或者直接引用 http://www.jq22.com/jquery-info122
將上述文件存儲到本地
html代碼
<div class="m-cell">
<div class="cell-item">
<div class="cell-left">所在地區:</div>
<div class="cell-right cell-arrow">
<input type="text" class="cell-input" readonly id="J_Address" value="" placeholder="請選擇收貨地址">
</div>
</div>
</div>
啓動JS代碼
!function () {
var $target = $('#J_Address');
$target.citySelect();
$target.on('click', function (event) {
event.stopPropagation();
$target.citySelect('open');
});
$target.on('done.ydui.cityselect', function (ret) {
console.log(ret)
console.log(ret.id);
$(this).val(ret.country + ' ' + ret.provance + ' ' + ret.city + ' ' + ret.area);
});
}();
獲取四級Json數據 地址的JAVA代碼
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
class AAA{
public String getJsonData(Long platformId) {
String json = "";
// 查詢所有省
List<Map<String, Object>> jsonList = new ArrayList<>();
// 獲取一地地址
List<BaseAddress> provList = baseAddressMapper.queryChildByCode("0");
for (BaseAddress provice : provList) {
JSONObject prov = new JSONObject();
prov.put("CountryId", provice.getCode());
prov.put("CountryName", provice.getName());
// 獲取二級地址
List<BaseAddress> cityList = baseAddressMapper.queryChildByCode(provice.getCode());
List<Map<String, Object>> cityListJson = new ArrayList<>();
for (BaseAddress c : cityList) {
JSONObject city = new JSONObject();
city.put("ProvinceId", c.getCode());
city.put("ProvinceName", c.getName());
// 獲取三級地址
List<BaseAddress> countyList = baseAddressMapper.queryChildByCode(c.getCode());
List<Map<String, Object>> countyListJson = new ArrayList<>();
for (BaseAddress coun : countyList) {
JSONObject county = new JSONObject();
county.put("CityId", coun.getCode());
county.put("CityName", coun.getName());
// 獲取四級地址
List<BaseAddress> townList = baseAddressMapper.queryChildByCode(coun.getCode());
List<Map<String, Object>> townListJson = new ArrayList<>();
for (BaseAddress t : townList) {
JSONObject town = new JSONObject();
town.put("CountyId", t.getCode());
town.put("CountyName", t.getName());
townListJson.add(town);
}
county.put("CountyList", townListJson);
countyListJson.add(county);
}
city.put("City", countyListJson);
cityListJson.add(city);
}
prov.put("Province", cityListJson);
jsonList.add(prov);
}
return JSONArray.toJSONString(jsonList);
}
}
說明下關鍵字段 和查詢的sql和 實體類
sql是按照 父Id查詢的List,
select * from address where parentCode=?
返回字段 主要包含兩個,
一個是name( provice.getName())
一個是 自身的id(provice.getCode()))
貼下實體類吧
public class BaseAddress implements Serializable {
private Long id;
private String code;
private String parentCode;
private String name;
/*省略getset*
/
}
===================發現按照操作也挺費勁的, 我還是自己打包了一份, ===========
===========有積分的還是自行下載吧 ,沒有積分的評論留郵箱, 我單獨發=========================