From 8be8c95d76bf7e805791e1c9ca1ddf680c02660d Mon Sep 17 00:00:00 2001 From: ‘chensiAb’ <‘chenchenco03@163.com’> Date: Wed, 16 Apr 2025 18:25:24 +0800 Subject: [PATCH] fix:地区组件-三级区域不存在时默认二级区域,删除无效代码 --- forms/chinaArea/CChinaArea.vue | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 42 insertions(+), 5 deletions(-) diff --git a/forms/chinaArea/CChinaArea.vue b/forms/chinaArea/CChinaArea.vue index 4eb81be..64ddc72 100644 --- a/forms/chinaArea/CChinaArea.vue +++ b/forms/chinaArea/CChinaArea.vue @@ -114,8 +114,9 @@ ); if (proviceIndex >= 0) { range[1] = locationTree[proviceIndex].children; - range[2] = + let cityChildren = locationTree[proviceIndex].children[0].children; + range[2] = cityChildren; current[0] = proviceIndex; // 市 if (codes[1]) { @@ -123,7 +124,19 @@ city => city.value === codes[1], ); if (cityIndex >= 0) { - range[2] = range[1][cityIndex].children; + let dists = range[1][cityIndex].children; + // 如果区域为空,添加默认的市级作为区域选项 + if (dists.length === 0) { + dists = [ + { + label: + range[1][cityIndex]?.label || '', + value: + range[1][cityIndex]?.value || '', + }, + ]; + } + range[2] = dists; current[1] = cityIndex; } // 区 @@ -162,7 +175,17 @@ // 第一列滚动 if (roll.column === 0) { const cities = locationTree[roll.value].children; - this.range = [this.range[0], cities, cities[0].children]; + let firstCityChildren = cities[0].children; + // 如果区域为空,添加默认的市级作为区域选项 + if (firstCityChildren.length === 0) { + firstCityChildren = [ + { + label: cities[0]?.label || '', + value: cities[0]?.value || '', + }, + ]; + } + this.range = [this.range[0], cities, firstCityChildren]; this.current = [roll.value, 0, 0]; if (this.level === 4) { const range3 = (cities[0].children[0] || {}).children || []; @@ -172,8 +195,21 @@ } // 第二列滚动 else if (roll.column === 1) { - const dists = + let dists = locationTree[this.current[0]].children[roll.value].children; + // 如果区域为空,添加默认的市级作为区域选项 + if (dists.length === 0) { + dists = [ + { + label: + locationTree[this.current[0]].children[roll.value] + ?.label || '', + value: + locationTree[this.current[0]].children[roll.value] + ?.value || '', + }, + ]; + } this.range = [this.range[0], this.range[1], dists]; this.current = [this.current[0], roll.value, 0]; if (this.level === 4) { @@ -207,7 +243,8 @@ const city = provice.children[detail[1]] || {}; names[1] = city.label; const dist = city.children[detail[2]] || {}; - names[2] = dist.label; + // 如果三级区域不存在或为空,则使用市的内容作为三级区域 + names[2] = dist.label || city.label; if (this.level === 4) { const street = (dist.children || [])[detail[3]] || {}; if (street.value) { -- Gitblit v1.9.1