|
|
|
|
@ -64,9 +64,46 @@ import {createLimitTree, getNodeId} from "@/util/tree" |
|
|
|
|
import {store, init} from '../store' |
|
|
|
|
import common from '../mixin' |
|
|
|
|
|
|
|
|
|
const PROVINCE_CITIES = ['北京市', '天津市', '上海市', '重庆市'] |
|
|
|
|
const PROVINCE_CITIES = [ |
|
|
|
|
{id: '11', name: '北京市'}, |
|
|
|
|
{id: '12', name: '天津市'}, |
|
|
|
|
{id: '31', name: '上海市'}, |
|
|
|
|
{id: '50', name: '重庆市'} |
|
|
|
|
] |
|
|
|
|
const DEFAULT_TABS = [{name: '省/直辖市'}, {name: '市'}, {name: '区/县'}, {name: '乡/镇/街道'}] |
|
|
|
|
|
|
|
|
|
//根据node的类型生成查找断言,支持{id:'10',...}、{name:'北京市',...}、'10'、'北京市' 等四种类型 |
|
|
|
|
function predicate(node) { |
|
|
|
|
if (typeof node === 'object') { |
|
|
|
|
const key = node.hasOwnProperty('id') ? 'id' : 'name' |
|
|
|
|
return item => item[key] === node[key] |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
const firstCharCode = node.charCodeAt(0) |
|
|
|
|
const key = 48 <= firstCharCode && firstCharCode <= 57 ? 'id' : 'name' |
|
|
|
|
return item => item[key] === node |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//根据叶子id获取树节点 |
|
|
|
|
function topDownById(tree, id) { |
|
|
|
|
//判断深度,深度=id.length/2 |
|
|
|
|
const depth = Math.floor(id.length / 2) |
|
|
|
|
if (depth < 1) return [] |
|
|
|
|
const result = [] |
|
|
|
|
for (let i = 1; i <= depth; i++) { |
|
|
|
|
const parentId = id.substring(0, i * 2) |
|
|
|
|
const parent = tree.find(node => node.id === parentId) |
|
|
|
|
if (!parent) return result |
|
|
|
|
result.push(parent) |
|
|
|
|
if (i === 1 && PROVINCE_CITIES.some(i => i.id === parentId)) { |
|
|
|
|
i++ |
|
|
|
|
} |
|
|
|
|
tree = parent.children || [] |
|
|
|
|
} |
|
|
|
|
return result |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default { |
|
|
|
|
name: "RegionTabSelector", |
|
|
|
|
|
|
|
|
|
@ -131,7 +168,6 @@ export default { |
|
|
|
|
|
|
|
|
|
clickItem(item) { |
|
|
|
|
this.setTabs(item, this.currentLevel) |
|
|
|
|
|
|
|
|
|
this.nextTab() |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
@ -142,7 +178,7 @@ export default { |
|
|
|
|
|
|
|
|
|
if (level === 1) { |
|
|
|
|
//选择直辖市的时候,最大深度-1,移除'市'tab |
|
|
|
|
if (PROVINCE_CITIES.includes(item.name)) { |
|
|
|
|
if (PROVINCE_CITIES.some(i => i.id === item.id)) { |
|
|
|
|
next.splice(1, 1) |
|
|
|
|
this.realMaxLevel = this.maxLevel - 1 |
|
|
|
|
} |
|
|
|
|
@ -189,10 +225,9 @@ export default { |
|
|
|
|
: this.value.split(this.separation).filter(Boolean) |
|
|
|
|
|
|
|
|
|
for (let i = 0; i < loopArray.length; i++) { |
|
|
|
|
const str = loopArray[i], |
|
|
|
|
parent = result[i - 1] || {children: this.treeData} |
|
|
|
|
|
|
|
|
|
const str = loopArray[i], parent = result[i - 1] || {children: this.treeData} |
|
|
|
|
const node = parent.children.find(predicate(str)) |
|
|
|
|
|
|
|
|
|
//只要有一次不满足就退出,保留之前的查找结果 |
|
|
|
|
if (!node) break |
|
|
|
|
|
|
|
|
|
@ -256,19 +291,6 @@ export default { |
|
|
|
|
this.init() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//根据node的类型生成查找断言,支持{id:'10',...}、{name:'北京市',...}、'10'、'北京市' 等四种类型 |
|
|
|
|
function predicate(node) { |
|
|
|
|
if (typeof node === 'object') { |
|
|
|
|
const key = node.hasOwnProperty('id') ? 'id' : 'name' |
|
|
|
|
return item => item[key] === node[key] |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
const firstCharCode = node.charCodeAt(0) |
|
|
|
|
const key = 48 <= firstCharCode && firstCharCode <= 57 ? 'id' : 'name' |
|
|
|
|
return item => item[key] === node |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
<style lang="scss" src="./style.scss"></style> |
|
|
|
|
|