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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<template>
<div style="width: auto; display: inline;">
<a-select :style="{width: width? width+ 'px':'80%'}" v-model="selected[key]" v-for="(arry, key) in selectArray" @focus="position = key" @change="selectChange($event, key)" :key="key" :disabled="disable && disabled[key]">
<a-select-option v-for="item in arry" :key="item.key" :value="item.key" select>{{ item.title }}</a-select-option>
</a-select>
</div>
</template>
<script>
// 用法 <unit-tree-select v-model="formData.treeCode" />
export default {
name: "UnitTreeSelect",
data () {
return {
unitList: [],
selectArray: [],
selected: [],
disabled: [],
defaultValue: {
title: "--请选择--",
key: "",
description: "",
selected: true,
disabled: true,
},
loadState: false,
};
},
props: {
value: {
type: String,
default: () => {
return ''
}
},
unitType: {
type: Number,
default: () => {
return null
}
},
defaultLength: {
type: Number,
default: () => {
return 5
}
},
title: {
type: String,
default () {
return ''
}
},
disable: {
type: Boolean,
default () {
return true
},
},
width: {
type: Number,
default () {
return 180
}
},
},
created () {
this.loadSelectValue(this.value, 1)
},
methods: {
loadSelectValue (value, type) {
if (!!!value)
value = ''
else
this.loadState = true
if (this.unitList.length == 0) {
this.$api.unit.getTreeCodeListByType({ unitType: this.unitType }).then(({ data = {} }) => {
if (data) {
this.unitList = data
this.selectArray = []
this.selected = []
this.disabled = []
this.loadDropDownList(value, type)
}
}).catch(() => { this.loading = false });
} else {
this.selectArray = []
this.selected = []
this.disabled = []
this.loadDropDownList(value, type)
}
},
loadDropDownList (code, type) {
let data = this.unitList.filter((item) => item.key.substring(0, item.key.length - this.defaultLength) === code)
if (data.length > 0) {
data.unshift(this.defaultValue)
this.selectArray.unshift(data)
if (code.length >= this.defaultLength) {
this.selected.unshift(code)
if (code.length == this.value.length) {
this.disabled.unshift(false)
} else {
this.disabled.unshift(true)
}
}
if ((!!this.value && this.value.length == code.length) || !!!this.value) {
this.selected.push('')
this.disabled.unshift(true)
}
} else {
this.selected.push(code)
this.disabled.unshift(true)
}
if (code.length >= this.defaultLength) {
this.loadDropDownList(code.substring(0, code.length - this.defaultLength), type);
}
},
treeDropDownSelect (code, index) {
let allArrCount = this.selectArray.length;
for (let i = index + 1; i < allArrCount; i++) {
this.selectArray.pop();
this.selected.pop();
this.disabled.pop();
}
if (code) {
let unitArr1 = this.unitList.filter((item) => item.key.substring(0, item.key.length - this.defaultLength) === code);
if (unitArr1.length > 0) {
unitArr1.unshift(this.defaultValue);
this.selectArray.push(unitArr1);
this.selected.push("");
this.disabled.push(false);
}
}
},
selectChange (value, key) {
this.treeDropDownSelect(value, key);
let allCount = this.selected.length;
let treeCode = "";
for (let i = allCount - 1; i >= 0; i--) {
if (this.selected[i] != "") {
treeCode = this.selected[i];
break;
}
}
this.$emit("input", treeCode);
this.$emit("change");
},
},
watch: {
value: {
handler (value) {
if (!!value && !this.loadState) {
this.loadSelectValue(value, 2)
this.loadState = true
}
},
},
}
}
</script>