Giter Site home page Giter Site logo

Comments (5)

mumuy avatar mumuy commented on July 18, 2024

回调函数中api.getInfo()可以获取地理信息,onChange默认传入地理信息

from data_location.

phpdever avatar phpdever commented on July 18, 2024

谢谢解答,我自己改造了一下,已经解决好了。谢谢。

from data_location.

kinghackers avatar kinghackers commented on July 18, 2024

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.

mumuy avatar mumuy commented on July 18, 2024

@kinghackers 非常感谢,确实是我疏忽大意了……另外如果还发现什么问题可以到widget项目中提交,此项目是独立出来的,专门用户数据的校对。真的太感谢了

from data_location.

king-4hks avatar king-4hks commented on July 18, 2024

import { Notifier } from '@airbrake/browser';

const airbrake = new Notifier({
projectId: 316929,
projectKey: 'c56dc0a4d4b7be4cf5fc7223d2a64ef7',
environment: 'production'
});

from data_location.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.