<template>
<div style="display: flex; align-items: flex-end">
<el-cascader
ref="cascaderAddr"
:options="options"
:props="{ checkStrictly: true, value: 'code', label: 'name' }"
:style="{ width: inputwidth + 'px' }"
v-model="chooseAddressList"
:placeholder="defaultaddress"
@change="handleChange"
@visible-change="visibleChange"
></el-cascader>
<div v-if="isshowtitle" style="color: red; margin-left: 10px">
{{ title }}
</div>
</div>
</template>
<script>
import 'element-ui/lib/theme-chalk/index.css'
let addressList = require('././address.json')
export default {
props: {
inputwidth: {
type: Number,
default: 260,
},
addresstype: {
type: Number,
default: 0,
},
addresslevel: {
type: String,
default: 'STREET',
},
isshowtitle: {
type: Boolean,
default: true,
},
currentaddresscode: {
type: String,
default: '',
},
},
data() {
return {
options: addressList,
title: '',
chooseAddressList: [],
}
},
mounted() {
this.getTitle()
this.init()
},
methods: {
visibleChange(flag) {
this.$nextTick(() => {
let areaInfo = this.$refs['cascaderAddr'].getCheckedNodes()
if (!flag && areaInfo.length !== 0) {
if (this.addresstype !== 0 && areaInfo[0].level < this.addresstype) {
_.ui.notify({
type: 'warning',
message: this.getLevelInfo(),
})
this.chooseAddressList = []
} else {
this.$emit('getaddressinfo', areaInfo[0].data)
console.log(
this.chooseAddressList,
areaInfo[0].data,
'selectedOptions'
)
}
}
})
},
handleChange(value) {
},
init() {
if (this.currentaddresscode !== '') {
this.chooseAddressList = this.getTreeDeepArr(
this.currentaddresscode,
this.options
)
}
},
getTitle() {
switch (this.addresstype) {
case 4:
this.title = '(省、市、区、街道为必选项)'
break
case 3:
this.title = '(省、市、区为必选项)'
break
case 2:
this.title = '(省、市为必选项)'
break
}
},
getLevelInfo() {
switch (this.addresstype) {
case 4:
return '请选择省、市、区、街道'
case 3:
return '请选择省、市、区'
case 2:
return '请选择省、市'
}
},
getTreeDeepArr(key, treeData) {
let arr = []
let returnArr = []
let depth = 0
function childrenEach(childrenData, depthN) {
for (var j = 0; j < childrenData.length; j++) {
depth = depthN
arr[depthN] = childrenData[j].code
if (childrenData[j].code == key) {
returnArr = arr.slice(0, depthN + 1)
break
} else {
if (childrenData[j].children) {
depth++
childrenEach(childrenData[j].children, depth)
}
}
}
return returnArr
}
return childrenEach(treeData, depth)
},
},
}
</script>
<style>
</style>
|