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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<template>
<div>
<el-select
v-if="itemObj.component && !readOnly"
:key="key"
v-model="selectValue"
v-bind="attrs"
:filterable="true"
@blur="(params)=>{emit('blur',params)}"
@focus="(params)=>{emit('focus',params)}"
@visibleChange="(params)=>{emit('visible-change',params)}"
>
<el-option
v-for="(option,index) in deeOptions"
:key="index"
:label="option[itemObj.component.labelWord || 'label']"
:value="option[itemObj.component.valueWord || 'value']"
v-bind="option"
/>
</el-select>
<span v-else-if="readOnly" style="line-height:40px">{{ showValue }}</span>
</div>
</template>
<script>
// import { getTableList } from '../../api/searchCom.js'
import { get, post } from '@/utils/http'
export default {
name: 'DeeSelectSearch',
components: {},
props: {
value: {
type: [Array, String, Boolean, Number],
default: () => ''
},
itemObj: {
type: Object,
default: () => {}
},
formValue: {
type: Object,
default: () => {}
},
readOnly: {
type: Boolean,
default: () => false
}
},
data() {
return {
key: 0,
selectValue: '',
parentValue: '',
deeOptions: [],
loading: false,
origeOPtions: [],
attrs: {}
}
},
computed: {
showValue() {
if (!this.deeOptions || this.deeOptions.length === 0) {
return ''
}
const params = this.deeOptions.find(r => r[this.itemObj.component.valueWord || 'value'] === this.value)
if (!params) {
return ''
}
return params[this.itemObj.component.labelWord || 'label']
}
},
watch: {
selectValue: {
deep: true,
handler: function(val) {
this.$emit('input', val)
this.$emit('change', val)
// 下拉选择器 配置另外输出值
// 此功能 只使用 自定义接口
if (this.itemObj.component.optionsId) {
if (val instanceof Array) { // 数组
const list = []
val.forEach(item => {
const option = this.deeOptions.find(r => r.value === val)
if (option) {
list.push(option[this.itemObj.component.optionsIdName || 'id'])
}
})
this.$set(this.formValue, this.itemObj.component.optionsId, list)
} else {
const option = this.deeOptions.find(r => r.value === val)
if (option) {
this.$set(this.formValue, this.itemObj.component.optionsId, option[this.itemObj.component.optionsIdName || 'id'])
}
}
}
}
},
value: {
immediate: true,
handler: function(val) {
this.$nextTick(() => {
this.selectValue = val
})
}
},
formValue: {
deep: true,
handler: function(newval, oldVal) {
const val = this.itemObj
if (val.component.dicType === '0' && val.component.optionsdic) {
const parentValue = this.getParentValue(this.itemObj.component.optionsFilter)
if (this.itemObj.component.optionsFilter && parentValue) {
if (this.parentValue !== parentValue) {
if (!this.readOnly && this.parentValue) {
this.selectValue = ''
}
this.getDictList(val)
}
} else if (this.itemObj.component.optionsFilter && (!newval || parentValue === '')) {
this.selectValue = ''
this.deeOptions = []
}
if (this.itemObj.component.optionsFilter) {
this.parentValue = parentValue
}
} else if (val.component.dicType === '3' && this.itemObj.component.optionsFilter && this.itemObj.component.optionsFilter) {
const parentValue = this.getParentValue(this.itemObj.component.optionsFilter)
if (this.parentValue !== parentValue) {
if (!this.readOnly && this.parentValue) {
this.selectValue = ''
}
this.getObjectAttr()
}
this.parentValue = parentValue
}
}
},
itemObj: {
immediate: true,
deep: true,
handler: function(val) {
this.attrs = JSON.parse(JSON.stringify(val.component))
if (this.attrs.hasOwnProperty('multiple')) {
this.attrs.multiple = Boolean(val.component.multiple)
this.key++
}
this.getOptions(val)
}
}
},
mounted: function() {
const val = this.itemObj
if (val && val.component) {
this.getOptions(val)
}
},
methods: {
emit(type, params) {
this.$emit(type, params)
},
getOptions(val) {
this.deeOptions = val.component && val.component.options ? JSON.parse(JSON.stringify(val.component.options)) : []
if (val.component.dicType === '0' && val.component.optionsdic) {
this.getDictList(val)
} else if (val.component.dicType === '1' && val.component.optionsObj) {
// this.deeOptions = []
} else if (val.component.dicType === '3') {
this.getObjectAttr()
}
},
async getObjectAttr() {
this.deeOptions = []
if (!this.itemObj || !this.itemObj.component) {
return
}
if (!this.itemObj.component.httpType || !this.itemObj.component.address) {
return
}
let fun = null
if (this.itemObj.component.httpType === 'get') {
fun = get
} else if (this.itemObj.component.httpType === 'post') {
fun = post
}
if (!fun) {
return
}
if (this.itemObj.component.paramsObj) {
this.getDeeOptionsByParamsObj(fun)
return
}
// 有父级 但父级没有选择 不读取本级下拉
if (this.itemObj.component.optionsFilter && (!this.formValue || !this.getParentValue(this.itemObj.component.optionsFilter))) {
return
}
const parentKey = this.itemObj.component.optionsFilter ? this.getParentValue(this.itemObj.component.optionsFilter) : null
this.deeOptions = await this.$utils.getSelectCustomApi(this.itemObj.key, parentKey, this.itemObj.component, this.formValue, this.itemObj.component.isCache)
},
getDeeOptionsByParamsObj(fun) {
const params = {}
this.itemObj.component.paramsObj && this.itemObj.component.paramsObj.forEach(attr => {
if (attr.paramsName === 'appId') {
params[attr.paramsName] = attr.paramsVal
} else {
params[attr.paramsName] = this.formValue && this.formValue[attr.paramsVal]
}
})
if (this.itemObj.component.paramsObj[0].paramsVal === 'optionsObj') {
if (!this.formValue['optionsObj']) {
return
}
}
fun(this.itemObj.component.address, params).then((res) => {
const items = res[this.itemObj.component.returnVal || 'items']
this.deeOptions = items.map(row => {
const item = {}
const label = this.itemObj.component.labelWord || 'label'
const value = this.itemObj.component.valueWord || 'value'
item[label] = row[label]
item[value] = row[value]
return item
})
})
},
getParentValue(optionsFilter) {
if (!this.formValue) {
return ''
}
if (!optionsFilter) {
return ''
}
if (optionsFilter.indexOf('.') < 0) {
return this.formValue[optionsFilter]
}
return this.formValue[optionsFilter] || this.formValue[optionsFilter.replace(/\./g, '|')]
},
setParams(list, filter, filedName) {
const items = []
list && list.forEach(item => {
if (item.fieldName && item.operator) {
const param = {
'fieldName': item.fieldName,
'operator': item.operator
}
if (item.type === 'form') {
param.value = this.formValue[item.formItem]
} else {
param.value = item.value
}
items.push(item)
}
})
if (filter && filedName) { // 级联过滤
items.push({
'fieldName': filedName,
'operator': 'EQ',
'value': this.getParentValue(filter)
})
}
return items
},
async getDictList(val) {
this.deeOptions = []
let code = ''
if (!val.component.optionsdic) {
return
} else if (val.component.optionsdic instanceof Array) {
code = val.component.optionsdic.slice(-1)[0]
} else {
code = val.component.optionsdic
}
let filter = ''
if (!val.component.optionsFilter) {
filter = ''
} else {
filter = this.getParentValue(val.component.optionsFilter)
}
// 有父级 但父级没有选择 不读取本级下拉
if (val.component.optionsFilter && (!this.formValue || !this.getParentValue(val.component.optionsFilter))) {
return
}
let list = await this.$utils.getDicListByCode(code, false, filter)
if (val.component.dicFilter && val.component.dicFilter.length && !val.component.optionsFilter) {
list = list.filter(r => val.component.dicFilter.includes(r.value))
}
this.$set(this, 'deeOptions', list)
}
// remoteMethod(query) {
// if (query !== '') {
// this.loading = true
// const formData = { name: query }
// get(this.itemObj.url || '/api/users', formData).then(res => {
// this.options = []
// if (res.items) {
// res.items.forEach(el => {
// this.options.push({
// value: el[this.itemObj.valueWord || 'id'],
// label: el[this.itemObj.labelWord || 'name']
// })
// })
// }
// })
// } else {
// this.options = []
// }
// }
},
filter: {}
}
</script>
<style scoped lang="scss">
</style>