From e44586ff0c279aac29e2f9ba7a1b19a97ecddbe4 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Mon, 21 Feb 2022 14:34:11 +0800 Subject: [PATCH] 优化地址三级联动组件,允许只有两级的地址,可以被选中 --- forms/chinaArea/ChinaLocations.js | 7 +++++-- forms/chinaArea/ChinaLocations.weapp.js | 7 +++++-- forms/chinaArea/CChinaArea.vue | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/forms/chinaArea/CChinaArea.vue b/forms/chinaArea/CChinaArea.vue index 087a6aa..85b8143 100644 --- a/forms/chinaArea/CChinaArea.vue +++ b/forms/chinaArea/CChinaArea.vue @@ -149,9 +149,9 @@ const codes = []; const provice = locationTree[detail[0]]; codes[0] = provice.value; - const city = provice.children[detail[1]]; + const city = provice.children[detail[1]] || []; codes[1] = city.value; - const area = city.children[detail[2]]; + const area = city.children[detail[2]] || []; codes[2] = area.value; this.itemRes.onChange(codes); }, diff --git a/forms/chinaArea/ChinaLocations.js b/forms/chinaArea/ChinaLocations.js index 61825ca..1d53f78 100644 --- a/forms/chinaArea/ChinaLocations.js +++ b/forms/chinaArea/ChinaLocations.js @@ -50,6 +50,9 @@ let address = ''; let tempLocationData = ChinaLocationData; regions.forEach((code) => { + if (!code || !tempLocationData[code]) { + return; + } if (typeof tempLocationData[code].name === 'string') { address += tempLocationData[code].name; } else { @@ -70,7 +73,7 @@ let address = []; let tempLocationData = ChinaLocationData; regions.forEach((code) => { - if (!tempLocationData[code]) { + if (!code || !tempLocationData[code]) { return; } if (typeof tempLocationData[code].name === 'string') { @@ -103,7 +106,7 @@ if (provinceChildren[cityCode].name === regions[1]) { codes[1] = cityCode; // 区 - const areaChildren = provinceChildren[cityCode].children; + const areaChildren = provinceChildren[cityCode].children || []; for (let areaCode in areaChildren) { if (areaChildren.hasOwnProperty(areaCode)) { if (areaChildren[areaCode] === regions[2]) { diff --git a/forms/chinaArea/ChinaLocations.weapp.js b/forms/chinaArea/ChinaLocations.weapp.js index 7c1986d..b8d80ca 100644 --- a/forms/chinaArea/ChinaLocations.weapp.js +++ b/forms/chinaArea/ChinaLocations.weapp.js @@ -74,6 +74,9 @@ let address = ''; let tempLocationData = ChinaLocationData; regions.forEach((code) => { + if (!code || !tempLocationData[code]) { + return; + } if (typeof tempLocationData[code].name === 'string') { address += tempLocationData[code].name; } else { @@ -94,7 +97,7 @@ let address = []; let tempLocationData = ChinaLocationData; regions.forEach((code) => { - if (!tempLocationData[code]) { + if (!code || !tempLocationData[code]) { return; } if (typeof tempLocationData[code].name === 'string') { @@ -128,7 +131,7 @@ if (provinceChildren[cityCode].name === regions[1]) { codes[1] = cityCode; // 区 - const areaChildren = provinceChildren[cityCode].children; + const areaChildren = provinceChildren[cityCode].children || []; for (let areaCode in areaChildren) { if (areaChildren.hasOwnProperty(areaCode)) { if (areaChildren[areaCode] === regions[2]) { -- Gitblit v1.9.1