Comments (5)
回调函数中api.getInfo()可以获取地理信息,onChange默认传入地理信息
from data_location.
谢谢解答,我自己改造了一下,已经解决好了。谢谢。
from data_location.
1、当required为true的时候,初始化默认选中值时省级不能选中(将format.province方法中添加“请选择”处的 options.province = 0; 注释掉即可);
2、初始化默认区划编码的地方建议改成如下,可解决当code为省或市级时,下一级选项没有默认值问题:
options.province = options.code - options.code%1e4;
options.city = options.code - (options.code%1e4 === 0 ? options.code : options.code%1e2);
options.area = options.code%1e2 === 0 ? 0 : options.code;
修改完成后的代码如下:
/**
- jquery.citys.js 1.0
-
http://jquerywidget.com
*/
;(function($, window, document, undefined) {
$.fn.citys = function(parameter,getApi) {
if(typeof parameter == 'function'){ //重载
getApi = parameter;
parameter = {};
}else{
parameter = parameter || {};
getApi = getApi||function(){};
}
var defaults = {
dataUrl:'public/tripartite/citys/citys.json', //数据库地址
provinceField:'province', //省份字段名
cityField:'city', //城市字段名
areaField:'area', //地区字段名
code:0, //地区编码
province:0, //省份,可以为地区编码或者名称
city:0, //城市,可以为地区编码或者名称
area:0, //地区,可以为地区编码或者名称
required: true, //是否必须选一个
nodata:'hidden', //当无数据时的表现形式
onChange:function(){} //地区切换时触发,回调函数传入地区数据
};
var options = $.extend({}, defaults, parameter);
if(options.code){ //如果设置地区编码,则忽略单独设置的信息
options.province = options.code - options.code%1e4;
options.city = options.code - (options.code%1e4 === 0 ? options.code : options.code%1e2);
options.area = (options.code%1e2 === 0 ? 0 : options.code);
}
return this.each(function() {
//对象定义
var _api = {};
var$this = $ (this);
var $province = $this.find('select[name="'+options.provinceField+'"]'),
$city = $this.find('select[name="'+options.cityField+'"]'),
$area = $this.find('select[name="'+options.areaField+'"]');
$.getJSON(options.dataUrl,function(data){
var province,city,area,hasCity;
var updateData = function(){
province = {},city={},area={};
hasCity = false; //判断是否有地级城市
for(code in data){
if(!(code%1e4)){ //获取所有的省级行政单位
province[code]=data[code];
if(options.required){
if(!options.province){
if(options.city&&!(options.city%1e4)){ //省未填,并判断为直辖市
options.province = options.city;
}else{
options.province = code;
}
}else if(data[code].indexOf(options.province)>-1){
options.province = isNaN(options.province)?code:options.province;
}
}
}else{
var p = code-options.province;
if(options.province&&p>0&&p<1e4){ //同省的城市或地区
if(!(code%100)){
hasCity = true;
city[code]=data[code];
if(options.required){
if(!options.city){
options.city = code;
}else if(data[code].indexOf(options.city)>-1){
options.city = isNaN(options.city)?code:options.city;
}
}
}else if(p>9000){ //省直辖县级行政单位
city[code]=data[code];
}else if(hasCity){ //非直辖市
var c = code-options.city;
if(options.city&&c>0&&c<100){ //同个城市的地区
area[code]=data[code];
if(options.required){
if(!options.area){
options.area = code;
}else if(data[code].indexOf(options.area)>-1){
options.area = isNaN(options.area)?code:options.area;
}
}
}
}else{
city[code]=data[code]; //直辖市
// if(options.required){ //这个判断其实没有必要
if(options.area){
options.city = options.area;
options.area = '';
}
if(!options.city){
options.city = code;
}else if(data[code].indexOf(options.city)>-1){
options.city = isNaN(options.city)?code:options.city;
}
// }
}
}
}
}
};
var format = {
province:function(){
$province.empty();
if(!options.required){
$province.append(' - 请选择 - ');
// options.province = 0;
}
for(i in province){
$province.append(''+province[i]+'');
}
if(options.province){
$province.val(options.province);
}
this.city();
},
city:function(){
$city.empty();
if(!options.required){
$city.append(' - 请选择 - ');
}
if(options.nodata=='disabled'){
$city.prop('disabled',$ .isEmptyObject(city));
}else{
$city.css('display',$ .isEmptyObject(city)?'none':'');
}
for(i in city){
$city.append(''+city[i]+'');
}
if(options.city){
$city.val(options.city);
}
this.area();
},
area:function(){
$area.empty();
if(!options.required){
$area.append(' - 请选择 - ');
}
if(options.nodata=='disabled'){
$area.prop('disabled',$ .isEmptyObject(area));
}else{
$area.css('display',$ .isEmptyObject(area)?'none':'');
}
for(i in area){
$area.append(''+area[i]+'');
}
if(options.area){
$area.val(options.area);
}
}
};
//获取当前地理信息
_api.getInfo = function(){
var status = {
direct:!hasCity,
province:data[options.province]||'',
city:data[options.city]||'',
area:data[options.area]||'',
code:options.area||options.city||options.province
};
return status;
};
//事件绑定
$province.on('change',function(){
options.province = $(this).val();
options.city = 0;
options.area = 0;
updateData();
format.city();
options.onChange(_api.getInfo());
});
$city.on('change',function(){
options.city = $(this).val();
options.area = 0;
updateData();
format.area();
options.onChange(_api.getInfo());
});
$area.on('change',function(){
options.area = $(this).val();
options.onChange(_api.getInfo());
});
//初始化
updateData();
format.province();
getApi(_api);
});
});
};
})(jQuery, window, document);
from data_location.
@kinghackers 非常感谢,确实是我疏忽大意了……另外如果还发现什么问题可以到widget项目中提交,此项目是独立出来的,专门用户数据的校对。真的太感谢了
from data_location.
import { Notifier } from '@airbrake/browser';
const airbrake = new Notifier({
projectId: 316929,
projectKey: 'c56dc0a4d4b7be4cf5fc7223d2a64ef7',
environment: 'production'
});
from data_location.
Related Issues (20)
- 区划数据可以配置历史版本吗? HOT 2
- 缺少 460400儋州市 620200嘉峪关市 419001济源市 441900东莞市 442000中山市 下数据 HOT 2
- 行政区已经改变了的,town文件夹中文件id对应区县district找不到 HOT 1
- 请问该数据库会持续更新么? HOT 1
- 能否导出一份sql文件 HOT 3
- 想问您一下,像这样的省市地址库,国家有相应的规范吗?比如数据库字段的命名,另外有没有官方的渠道可以系统获取这些规范的地址信息呢? HOT 1
- 我是个小白,想下shp格式的,怎么下呀 HOT 2
- 少了一个镇 HOT 1
- 太谷区没有对应乡镇数据 HOT 3
- 数据库好像请求不了 HOT 1
- 区对应街道多文件问题 HOT 2
- 你好 湖北部分地区地址更新了 是否可以做一个数据的update呢? HOT 1
- 安徽省芜湖市无为市没有四级地区 HOT 2
- 630106.json不存在 HOT 2
- https://passer-by.com/data_location/list.json 手机联通用户访问不了 HOT 1
- 441900.json 442000.json 里面的城镇是位数比其他的多 HOT 1
- 没有河北省邢台市新调整的两个区:南和区(130506.json)和任泽区(130505.json)的两个json文件,里面已经做好内容,只需要添加上即可
- 数据重复 HOT 1
- 360112.json不存在 HOT 1
- 请增加直辖市的中间层 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data_location.