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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| <template>
| <el-form-item :label="placeholder" :prop="prop">
| <el-cascader
| :model-value="formatedValue"
| @change="handleChange"
| :options="locations"
| :placeholder="placeholder"
| :props="optionProps"
| style="width: 280px"
| />
| </el-form-item>
| </template>
|
| <script>
| import { enumLocation } from '@/enum/location';
|
| export default {
| props: {
| // 是否在首选项处添加“全部”选项
| allOption: {
| type: Boolean,
| default: true
| },
| // 查询的行政级别,取值1,2,3,4
| level: {
| type: Number,
| default: 4
| },
| // 结果返回
| value: Object,
| // 是否默认返回初始选项
| initValue: {
| type: Boolean,
| default: true
| },
| // 能否选择任意一级选项
| checkStrictly: {
| type: Boolean,
| default: true
| },
| prop: String
| },
| emits: ['update:value'],
| data() {
| return {
| locations: enumLocation(this.allOption, this.level),
| optionProps: {
| checkStrictly: this.checkStrictly
| }
| };
| },
| computed: {
| placeholder() {
| const list = '省/市/区/镇'.split('/');
| const p = [];
| for (let i = 0; i < this.level; i++) {
| p.push(list[i]);
| }
| return p.join('/');
| },
| formatedValue() {
| return this.optionFormatReverse(this.value);
| }
| },
| methods: {
| handleChange(value) {
| this.$emit('update:value', this.optionFormat(value));
| },
| /**
| * 地区选项结果格式化
| */
| optionFormat(val) {
| const res = {
| pCode: null,
| pName: null,
| cCode: null,
| cName: null,
| dCode: null,
| dName: null,
| tCode: null,
| tName: null
| };
| if (val.length > 0) {
| res.pCode = val[0][0];
| res.pName = val[0][1];
| }
| if (val.length > 1) {
| res.cCode = val[1][0];
| res.cName = val[1][1];
| }
| if (val.length > 2) {
| res.dCode = val[2][0];
| res.dName = val[2][1];
| }
| if (val.length > 3) {
| res.tCode = val[3][0];
| res.tName = val[3][1];
| }
| return res;
| },
| optionFormatReverse(val) {
| const res = [];
| if (val) {
| if (val.pName) {
| res.push([val.pCode, val.pName]);
| }
| if (val.cName) {
| res.push([val.cCode, val.cName]);
| }
| if (val.dName) {
| res.push([val.dCode, val.dName]);
| }
| if (val.tName) {
| res.push([val.tCode, val.tName]);
| }
| }
| return res;
| }
| },
| mounted() {
| if (this.initValue) {
| if (this.checkStrictly) {
| this.handleChange([this.locations[0].value]);
| } else {
| const f = (location) => {
| if (location.children && location.children.length > 0) {
| const r = f(location.children[0]);
| r.unshift(location.value);
| return r;
| } else {
| return [location.value];
| }
| };
| this.handleChange(f(this.locations[0]));
| }
| }
| }
| };
| </script>
|
|