|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<template>
<a-tree-select
allowClear
labelInValue
:getPopupContainer="(node) => node.parentNode"
style="width: 100%"
:disabled="disabled"
:dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
:placeholder="placeholder"
:loadData="asyncLoadTreeData"
:value="treeValue"
:treeData="treeData"
:multiple="multiple"
@change="onChange"
@search="onSearch">
</a-tree-select>
</template>
<script>
|
|
20
21
22
23
24
|
/*
* 异步树加载组件 通过传入表名 显示字段 存储字段 加载一个树控件
* <j-tree-select dict="aa_tree_test,aad,id" pid-field="pid" ></j-tree-select>
* */
import {getAction} from '@/api/manage'
|
|
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
|
export default {
name: 'JTreeSelect',
props: {
value: {
type: String,
required: false
},
placeholder: {
type: String,
default: '请选择',
required: false
},
dict: {
type: String,
default: '',
required: false
},
pidField: {
type: String,
default: 'pid',
required: false
},
pidValue: {
type: String,
default: '',
required: false
},
disabled: {
type: Boolean,
default: false,
required: false
|
|
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
|
hasChildField: {
type: String,
default: '',
required: false
},
condition: {
type: String,
default: '',
required: false
},
// 是否支持多选
multiple: {
type: Boolean,
default: false,
},
loadTriggleChange: {
type: Boolean,
default: false,
required: false
}
},
data() {
return {
treeValue: null,
treeData: [],
url: "/sys/dict/loadTreeData",
view: '/sys/dict/loadDictItem/',
tableName: "",
text: "",
code: "",
|
|
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
|
}
},
watch: {
value() {
this.loadItemByCode()
},
dict() {
this.initDictInfo()
this.loadRoot();
}
},
created() {
this.validateProp().then(() => {
this.initDictInfo()
this.loadRoot()
this.loadItemByCode()
})
},
methods: {
loadItemByCode() {
if (!this.value || this.value == "0") {
this.treeValue = null
} else {
getAction(`${this.view}${this.dict}`, {key: this.value}).then(res => {
if (res.success) {
let values = this.value.split(',')
this.treeValue = res.result.map((item, index) => ({
key: values[index],
value: values[index],
label: item
}))
this.onLoadTriggleChange(res.result[0]);
}
})
|
|
123
124
|
}
},
|
|
125
126
127
128
|
onLoadTriggleChange(text) {
//只有单选才会触发
if (!this.multiple && this.loadTriggleChange) {
this.$emit('change', this.value, text)
|
|
129
130
|
}
},
|
|
131
132
133
134
135
|
initDictInfo() {
let arr = this.dict.split(",")
this.tableName = arr[0]
this.text = arr[1]
this.code = arr[2]
|
|
136
|
},
|
|
137
138
139
140
141
|
asyncLoadTreeData(treeNode) {
return new Promise((resolve) => {
if (treeNode.$vnode.children) {
resolve()
return
|
|
142
|
}
|
|
143
144
145
146
147
148
149
150
151
|
let pid = treeNode.$vnode.key
let param = {
pid: pid,
tableName: this.tableName,
text: this.text,
code: this.code,
pidField: this.pidField,
hasChildField: this.hasChildField,
condition: this.condition
|
|
152
|
}
|
|
153
154
155
156
157
158
159
160
|
getAction(this.url, param).then(res => {
if (res.success) {
for (let i of res.result) {
i.value = i.key
if (i.leaf == false) {
i.isLeaf = false
} else if (i.leaf == true) {
i.isLeaf = true
|
|
161
162
|
}
}
|
|
163
164
165
166
|
this.addChildren(pid, res.result, this.treeData)
this.treeData = [...this.treeData]
}
resolve()
|
|
167
|
})
|
|
168
169
170
171
172
173
174
175
176
177
|
})
},
addChildren(pid, children, treeArray) {
if (treeArray && treeArray.length > 0) {
for (let item of treeArray) {
if (item.key == pid) {
if (!children || children.length == 0) {
item.isLeaf = true
} else {
item.children = children
|
|
178
|
}
|
|
179
180
181
|
break
} else {
this.addChildren(pid, children, item.children)
|
|
182
183
|
}
}
|
|
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
}
},
loadRoot() {
let param = {
pid: this.pidValue,
tableName: this.tableName,
text: this.text,
code: this.code,
pidField: this.pidField,
hasChildField: this.hasChildField,
condition: this.condition
}
getAction(this.url, param).then(res => {
if (res.success && res.result) {
for (let i of res.result) {
i.value = i.key
if (i.leaf == false) {
i.isLeaf = false
} else if (i.leaf == true) {
i.isLeaf = true
|
|
204
205
|
}
}
|
|
206
|
this.treeData = [...res.result]
|
|
207
|
} else {
|
|
208
|
console.log("数根节点查询结果-else", res)
|
|
209
|
}
|
|
210
211
212
213
214
215
216
217
218
219
220
221
222
|
})
},
onChange(value) {
if (!value) {
this.$emit('change', '');
this.treeValue = null
} else if (value instanceof Array) {
this.$emit('change', value.map(item => item.value).join(','))
this.treeValue = value
} else {
this.$emit('change', value.value, value.label)
this.treeValue = value
}
|
|
223
|
|
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
},
onSearch(value) {
console.log(value)
},
getCurrTreeData() {
return this.treeData
},
validateProp() {
let mycondition = this.condition
return new Promise((resolve, reject) => {
if (!mycondition) {
resolve();
} else {
try {
let test = JSON.parse(mycondition);
if (typeof test == 'object' && test) {
resolve()
} else {
|
|
242
243
244
|
this.$message.error("组件JTreeSelect-condition传值有误,需要一个json字符串!")
reject()
}
|
|
245
246
247
|
} catch (e) {
this.$message.error("组件JTreeSelect-condition传值有误,需要一个json字符串!")
reject()
|
|
248
|
}
|
|
249
250
|
}
})
|
|
251
|
}
|
|
252
253
254
255
256
|
},
//2.2新增 在组件内定义 指定父组件调用时候的传值属性和事件类型 这个牛逼
model: {
prop: 'value',
event: 'change'
|
|
257
|
}
|
|
258
|
}
|
|
259
|
</script>
|