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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<template>
<div class='app-upicker'>
<el-select ref="appUpicker" :value="refvalue" size='small' filterable
@change="onSelect" :disabled="disabled" style='width:100%;' clearable
@clear="onClear" @visible-change="onSelectOpen">
<template v-if="items">
<el-option v-for="(_item,index) in items" :key="index" :value="_item.value" :label="_item.label"></el-option>
</template>
</el-select>
<span style='position: absolute;right: 5px;color: #c0c4cc;top:0;font-size: 13px;'>
<i v-show="open" class='el-icon-arrow-up' @click="closeDropdown"></i>
<i v-show="!open" class='el-icon-arrow-down' @click="openDropdown"></i>
</span>
</div>
</template>
<script lang = 'ts'>
import { Component, Vue, Prop, Model, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { AppModal } from '@/utils';
@Component({
})
export default class AppUpicker extends Vue {
/**
* 请求到的数据
* @type {any[]}
* @memberof AppUpicker
*/
public itemList: any[] = []
/**
* 列表项请求路径
*
* @type {string}
* @memberof AppUpicker
*/
public url: string = '';
/**
* 请求参数和请求数据的映射关系
*
* @type {*}
* @memberof AppUpicker
*/
public interaction:any = {};
/**
* 编辑器参数
*
* @type {*}
* @memberof AppUpicker
*/
@Prop() public itemParams?: any;
/**
* 视图上下文
*
* @type {*}
* @memberof AppUpicker
*/
@Prop() public context!: any;
/**
* 视图参数
*
* @type {*}
* @memberof AppUpicker
*/
@Prop() public viewparams!: any;
/**
* 表单数据
*
* @type {*}
* @memberof AppUpicker
*/
@Prop() public data!: any;
/**
* 属性项名称
*
* @type {string}
* @memberof AppUpicker
*/
@Prop() public name!: string;
/**
* 是否启用
*
* @type {boolean}
* @memberof AppUpicker
*/
@Prop() public disabled?: boolean;
/**
* 是否显示按钮
*
* @type {boolean}
* @memberof AppUpicker
*/
@Prop({default:true}) public showButton?: boolean;
/**
* 局部上下文导航参数
*
* @type {any}
* @memberof AppUpicker
*/
@Prop() public localContext!:any;
/**
* 局部导航参数
*
* @type {any}
* @memberof AppUpicker
*/
@Prop() public localParam!:any;
/**
* 值项名称
*
* @type {string}
* @memberof AppUpicker
*/
@Prop() public valueitem!: string;
/**
* 排序
*
* @type {string}
* @memberof AppUpicker
*/
@Prop() public sort?: string;
/**
* 值
*
* @type {*}
* @memberof AppUpicker
*/
@Model('change') public value?: any;
/**
* 当前值
*
* @type {string}
* @memberof AppUpicker
*/
public curvalue: string = '';
/**
* 下拉数组
* @type {any[]}
* @memberof AppUpicker
*/
public items: any[] = [];
/**
* 下拉图标指向状态管理
* @type {boolean}
* @memberof AppUpicker
*/
public open: boolean = false;
/**
* 输入状态
*
* @type {boolean}
* @memberof AppUpicker
*/
public inputState: boolean = false;
/**
* vue 生命周期
*
* @memberof AppUpicker
*/
public created() {
this.analysis(this.itemParams);
}
/**
* 获取关联数据项值
*
* @readonly
* @memberof AppUpicker
*/
get refvalue() {
if (this.valueitem && this.data) {
return this.data[this.valueitem];
}
return this.curvalue;
}
/**
* 展开下拉
*
* @memberof AppUpicker
*/
public openDropdown() {
const appUpicker: any = this.$refs.appUpicker;
if(appUpicker) {
appUpicker.focus();
}
}
/**
* 收起下拉
*
* @memberof AppUpicker
*/
public closeDropdown() {
const appUpicker: any = this.$refs.appUpicker;
if(appUpicker) {
appUpicker.blur();
}
}
/**
* 下拉切换回调
* @param flag
*
* @memberof AppUpicker
*/
public onSelectOpen(flag: boolean): void {
this.open = flag;
if (this.open) {
this.fectchItemList(this.url);
}
}
/**
* 下拉选中
*
* @param {string} val
* @memberof AppUpicker
*/
public onSelect(val: string) {
let index = this.items.findIndex((item) => Object.is(item.value, val));
if (index >= 0) {
let item:any = this.items[index];
if (this.valueitem) {
this.$emit('formitemvaluechange', { name: this.valueitem, value: item.value });
}
if (this.name) {
this.$emit('formitemvaluechange', { name: this.name, value: item.label });
}
}
}
/**
* 清除
*
* @memberof AppUpicker
*/
public onClear($event: any): void {
if (this.valueitem) {
this.$emit('formitemvaluechange', { name: this.valueitem, value: '' });
}
if (this.name) {
this.$emit('formitemvaluechange', { name: this.name, value: '' });
}
this.$forceUpdate();
}
/**
* 值变化
*
* @param {*} newVal
* @param {*} oldVal
* @memberof AppUpicker
*/
@Watch('value',{immediate:true})
public onValueChange(newVal: any, oldVal: any) {
this.analysis(this.itemParams);
if(newVal){
this.curvalue = newVal;
const value = this.data[this.valueitem];
const index = this.items.findIndex((item: any) => Object.is(item.value, value));
if (index !== -1) {
return;
}
this.items = [];
if (value) {
this.items.push({text: newVal, value: value});
}
}
}
/**
* 解析编辑器参数
* @param {*} itemparams
*
* @memberof AppUpicker
*/
public analysis(itemparams:any) {
Object.keys(itemparams).forEach((param)=>{
if(param==='path'){
this.url = itemparams[param]
}else{
this.interaction[param] = itemparams[param]
}
})
}
/**
* 请求下拉列表数据
* @param {string} url
*
* @memberof AppUpicker
*/
public fectchItemList(url:string) {
let arg:any = {};
this.handlePublicParams(arg);
this.$http
.get(url,arg.param)
.then((response: any) => {
if (response && response.status==200 && response.data) {
this.itemList = response.data;
// 提取需要的值(value,label)
this.extractItem(this.itemList,this.items);
}
})
.catch((response: any) => {
if (!response || !response.status || !response.data) {
this.$Notice.error({
title: this.$t("app.commonWords.error") as string,
desc: this.$t("app.commonWords.sysException") as string,
});
return;
}
});
}
/**
* 解析下拉列表数据
* @param {any[]} itemList 请求到的数据
* @param {any[]} items 下拉数组
*
* @memberof AppUpicker
*/
public extractItem(itemList:any[],items:any[]) {
this.items = [];
itemList.forEach((item) => {
this.items.push({
label: item[this.interaction.label],
value: item[this.interaction.value],
});
});
}
/**
* 公共参数处理
*
* @param {*} arg
* @returns
* @memberof AppUpicker
*/
public handlePublicParams(arg: any): boolean {
if (!this.data) {
this.$Notice.error({ title: (this.$t('components.appPicker.error') as any), desc: (this.$t('components.appPicker.formdataException') as any) });
return false;
}
// 合并表单参数
arg.param = this.viewparams ? JSON.parse(JSON.stringify(this.viewparams)) : {};
arg.context = this.context ? JSON.parse(JSON.stringify(this.context)) : {};
// 附加参数处理
if (this.localContext && Object.keys(this.localContext).length >0) {
let _context = this.$util.computedNavData(this.data,arg.context,arg.param,this.localContext);
Object.assign(arg.context,_context);
}
if (this.localParam && Object.keys(this.localParam).length >0) {
let _param = this.$util.computedNavData(this.data,arg.param,arg.param,this.localParam);
Object.assign(arg.param,_param);
}
return true;
}
}
</script>
<style lang="less">
@import './app-upicker.less';
</style>