index.vue
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<template>
<el-cascader v-model="city" :options="cityList" :props="cascaderProps" :disabled="disabled" @change="onChange"></el-cascader>
</template>
<script>
import cityJson from "@/assets/json/city.json";
export default {
name: "CitySelect",
props: {
value: {
type: [String, Number],
default: "",
},
checkStrictly: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
},
data() {
return {
cityList: cityJson,
city: "",
cascaderProps: {
value: "code",
label: "name",
children: "children",
checkStrictly: this.checkStrictly,
},
};
},
watch: {
value: {
handler: function (n, o) {
if (n) {
this.init();
}
},
immediate: true,
},
},
mounted() {
//this.city = this.value;
},
methods: {
onChange(e) {
console.log(e);
let list = this.cityList;
let m = list.find((k) => k.code == e[0]);
let n = m.children.find((k) => k.code == e[1]);
let b = (n.children || []).find((k) => k.code == e[2]);
if (n) {
this.$emit("input", n.name);
}
this.$emit("change", { code: e, name: [m.name, n.name, b.name] });
},
// 初始化
init() {
let o = this.value;
let list = this.cityList;
let arr = [];
if (!o) {
return false;
}
let m = list.find((k) => {
let n = k.children.find((j) => j.name == o);
if (n) {
arr.push(k.code);
arr.push(n.code);
}
});
this.city = arr;
console.log(arr);
},
},
};
</script>
<style lang="scss" scoped>
</style>