From 808198f15ce090114329d253e8b809f64452b028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B1=E7=9C=A0=E4=BB=AA=E5=BC=8F?= <854085467@qq.com> Date: Wed, 3 Feb 2021 12:46:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20area=20=E7=9C=81=E5=B8=82?= =?UTF-8?q?=E7=BA=A7=E8=81=94=E7=BB=84=E4=BB=B6,=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=A4=9A=E5=AE=9E=E4=BE=8B=E6=95=B0=E6=8D=AE=E9=9A=94=E7=A6=BB?= =?UTF-8?q?=E9=97=AE=E9=A2=98,=20=E4=BC=98=E5=8C=96=E7=BA=A7=E8=81=94?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/pear/modules/area.js | 118 +++++++++++++++++++++++---------- config/pear.config.yml | 2 +- view/document/area.html | 12 ++-- 3 files changed, 91 insertions(+), 41 deletions(-) diff --git a/component/pear/modules/area.js b/component/pear/modules/area.js index 2a0c4c2..604c91e 100644 --- a/component/pear/modules/area.js +++ b/component/pear/modules/area.js @@ -22,11 +22,23 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { that.layarea.call(that, files); } , config: that.config + , reload: that.reload + , events: that.events } } , Class = function (options) { let that = this; - that.config = $.extend({}, that.config, layarea.config, options); + that.config = $.extend({}, { + elem: '', + data: { + province: '', + city: '', + county: '', + provinceCode: 0, + cityCode: 0, + countyCode: 0, + } + }, options); that.render(); }; @@ -3819,9 +3831,12 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { Class.prototype.config = { elem: '', data: { - province: '--选择省--', - city: '--选择市--', - county: '--选择区--', + province: '', + city: '', + county: '', + provinceCode: 0, + cityCode: 0, + countyCode: 0, }, change: function(result){} }; @@ -3831,13 +3846,18 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { Class.prototype.render = function () { let that = this, options = that.config; options.elem = $(options.elem); - options.bindAction = $(options.bindAction); that.events(); }; + + Class.prototype.reload = function (op) { + let options = this.config; + options.data = $.extend(options.data, op.data || {}); + this.events(true); + }; - Class.prototype.events = function () { - let that = this, options = that.config, index; + Class.prototype.events = function (reload = false) { + let that = this, options = that.config; let provinceFilter = 'province-' + layarea._id; let cityFilter = 'city-' + layarea._id; let countyFilter = 'county-' + layarea._id; @@ -3845,7 +3865,15 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { let provinceEl = options.elem.find('.province-selector'); let cityEl = options.elem.find('.city-selector'); let countyEl = options.elem.find('.county-selector'); - + + if (reload){ + options.data.provinceCode = getCode('province', options.data.province); + let code = getCode('city', options.data.city, options.data.provinceCode.slice(0, 2)); + options.data.cityCode = code; + options.data.countyCode = getCode('county', options.data.county, options.data.cityCode.slice(0, 4)); + renderProvince(); + return; + } //filter if(provinceEl.attr('lay-filter')){ provinceFilter = provinceEl.attr('lay-filter'); @@ -3863,35 +3891,49 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { //获取默认值 if(provinceEl.data('value')){ options.data.province = provinceEl.data('value'); + options.data.provinceCode = getCode('province', options.data.province); + } else if (!options.data.province) { + options.data.province = ''; } if(cityEl.data('value')){ options.data.city = cityEl.data('value'); + let code = getCode('city', options.data.city, options.data.provinceCode.slice(0, 2)); + options.data.cityCode = code; + } else if (!options.data.city) { + options.data.city = ''; } if(countyEl.data('value')){ options.data.county = countyEl.data('value'); + options.data.countyCode = getCode('county', options.data.county, options.data.cityCode.slice(0, 4)); + } else if (!options.data.county) { + options.data.county = ''; } provinceEl.attr('lay-filter', provinceFilter); cityEl.attr('lay-filter', cityFilter); countyEl.attr('lay-filter', countyFilter); - + //监听结果 form.on('select('+provinceFilter+')', function(data){ options.data.province = data.value; - let code = getCode('province', data.value); - renderCity(code); + options.data.provinceCode = getCode('province', data.value); + renderCity(options.data.provinceCode); options.change(options.data); }); form.on('select('+cityFilter+')', function(data){ options.data.city = data.value; - let code = getCode('city', data.value); - renderCounty(code); + if(options.data.provinceCode){ + options.data.cityCode = getCode('city', data.value, options.data.provinceCode.slice(0, 2)); + renderCounty(options.data.cityCode); + } options.change(options.data); }); form.on('select('+countyFilter+')', function(data){ options.data.county = data.value; - + if(options.data.cityCode){ + options.data.countyCode = getCode('county', data.value, options.data.cityCode.slice(0, 4)); + } options.change(options.data); }); @@ -3899,22 +3941,21 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { //查找province function renderProvince(){ - let tpl = ''; + let tpl = ''; let provinceList = getList("province"); let currentCode = ''; let currentName = ''; provinceList.forEach(function(_item){ - if (!currentCode){ - currentCode = _item.code; - currentName = _item.name; - } + // if (!currentCode){ + // currentCode = _item.code; + // currentName = _item.name; + // } if(_item.name === options.data.province){ currentCode = _item.code; currentName = _item.name; } tpl += ''; }); - options.data.province = currentName; provinceEl.html(tpl); provinceEl.val(options.data.province); form.render('select'); @@ -3922,15 +3963,15 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { } function renderCity(provinceCode){ - let tpl = ''; + let tpl = ''; let cityList = getList('city', provinceCode.slice(0, 2)); let currentCode = ''; let currentName = ''; cityList.forEach(function(_item){ - if (!currentCode){ - currentCode = _item.code; - currentName = _item.name; - } + // if (!currentCode){ + // currentCode = _item.code; + // currentName = _item.name; + // } if(_item.name === options.data.city){ currentCode = _item.code; currentName = _item.name; @@ -3945,15 +3986,15 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { } function renderCounty(cityCode){ - let tpl = ''; + let tpl = ''; let countyList = getList('county', cityCode.slice(0, 4)); let currentCode = ''; let currentName = ''; countyList.forEach(function(_item){ - if (!currentCode){ - currentCode = _item.code; - currentName = _item.name; - } + // if (!currentCode){ + // currentCode = _item.code; + // currentName = _item.name; + // } if(_item.name === options.data.county){ currentCode = _item.code; currentName = _item.name; @@ -3996,15 +4037,25 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { return result; } - function getCode(type, name){ + function getCode(type, name, parentCode = 0){ let code = ''; let list = areaList[type + "_list"] || {}; - layui.each(list, function(_code, _name){ + let result = {}; + Object.keys(list).map(function (_code) { + if(parentCode){ + if(_code.indexOf(parentCode) === 0){ + result[_code] = list[_code]; + } + }else{ + result[_code] = list[_code]; + } + }); + layui.each(result, function(_code, _name){ if(_name === name){ code = _code; } }); - + return code; } }; @@ -4015,6 +4066,5 @@ layui.define(['layer', 'form', 'laytpl'], function (exports) { return thisArea.call(inst); }; - //暴露接口 exports('area', layarea); }); \ No newline at end of file diff --git a/config/pear.config.yml b/config/pear.config.yml index abb360b..a5ab10b 100644 --- a/config/pear.config.yml +++ b/config/pear.config.yml @@ -45,7 +45,7 @@ colors: - id: "1" color: "#2d8cf0" - id: "2" - color: "#5FB878" + color: "#36b368" - id: "3" color: "#f6ad55" - id: "4" diff --git a/view/document/area.html b/view/document/area.html index 2ba4dab..ec75d75 100644 --- a/view/document/area.html +++ b/view/document/area.html @@ -41,17 +41,17 @@
网点地址
-
-
-
@@ -70,17 +70,17 @@
网点地址
-
-
-