From f20ffda48fd8baa97bd56b0cdb0584dacd7600dd Mon Sep 17 00:00:00 2001 From: macazy <393026117@qq.com> Date: Sat, 27 May 2017 12:57:30 +0800 Subject: [PATCH] Update city-picker.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加参数 whiteList用于限制可选城市 如 whiteList:[{code:"610000",sub:["610100"]}] 仅能选择陕西省西安市 whiteList:[{code:"610000"}]可以选择陕西省下全部城市 默认 whiteList:[] 可以选择全部省份城市 --- src/js/city-picker.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/js/city-picker.js b/src/js/city-picker.js index 0f4fd2e..d5f275f 100644 --- a/src/js/city-picker.js +++ b/src/js/city-picker.js @@ -65,6 +65,40 @@ $.fn.cityPicker = function(params) { params = $.extend({}, defaults, params); + var whiteList = params.whiteList || []; //白名单配置 + if(!!whiteList && whiteList.length > 0){ + var genWhiteListProvinceSubData = function(code,subArray){ + var res = []; + for(var i in whiteList){ + if(whiteList[i].code == code){ + if(!!whiteList[i].sub && whiteList[i].sub.length > 0){ + for(var o in subArray){ + if($.inArray(subArray[o].code, whiteList[i].sub) != -1){ + res.push(subArray[o]); + } + } + }else{ + res = subArray; + } + break; + } + } + return res; + }; + var whiteListProvince = whiteList.map(function(d){return d.code}); + var renderCitiesData = []; + for(var i in raw){ + if($.inArray(raw[i].code,whiteListProvince) != -1){ + var province = { + name:raw[i].name, + code:raw[i].code, + sub:genWhiteListProvinceSubData(raw[i].code,raw[i].sub) + }; + renderCitiesData.push(province); + } + } + if(renderCitiesData.length != 0) raw = renderCitiesData; + } return this.each(function() { var self = this;