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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<template>
<el-autocomplete
class="app-picker-microservice"
:disabled="disabled"
v-model="curvalue"
size="small"
:readonly="readonly"
:placeholder="placeholder"
:trigger-on-focus="true"
:popper-class="popperClass"
:fetch-suggestions="onSearch"
@select="onSelect"
>
<template v-slot:suffix>
<i v-if="curvalue && !disabled" class="el-icon-circle-close" @click="onClear"></i>
<i class="el-icon-arrow-down"></i>
</template>
<template slot-scope="{ item }">
<template v-if="Object.is(mode, 'tree')">
<el-tree
ref="tree"
:data="treeData"
:show-checkbox="multiple"
:node-key="showValue"
:props="defaultProps"
:check-strictly="true"
:default-checked-keys="defaultKeys"
:filter-node-method="filterNode"
:check-on-click-node="true"
@check="onTreeSelect"
></el-tree>
</template>
<template v-else>
<span>{{ item.label }}</span>
</template>
</template>
</el-autocomplete>
</template>
<script lang="ts">
import { Util } from 'ibiz-core';
import { Subject, Subscription } from 'rxjs';
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
@Component({})
export default class AppPickerMicroservice extends Vue {
/**
* 视图上下文
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public context!: any;
/**
* 视图参数
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public viewparams!: any;
/**
* 表单服务
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public service?: any;
/**
* 应用实体主信息属性名称
*
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop({ default: 'srfmajortext' }) public deMajorField!: string;
/**
* 应用实体主键属性名称
*
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop({ default: 'srfkey' }) public deKeyField!: string;
/**
* 表单数据
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public data!: any;
/**
* 属性项名称
*
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop() public name!: string;
/**
* 是否启用
*
* @type {boolean}
* @memberof AppPickerMicroservice
*/
@Prop() public disabled?: boolean;
/**
* 只读模式
* @type {boolean}
* @memberof AppPickerMicroservice
*/
@Prop({ default: false }) public readonly?: boolean;
/**
* 局部上下文导航参数
*
* @type {any}
* @memberof AppPickerMicroservice
*/
@Prop() public localContext!: any;
/**
* 局部导航参数
*
* @type {any}
* @memberof AppPickerMicroservice
*/
@Prop() public localParam!: any;
/**
* 值项名称
*
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop() public valueitem!: string;
/**
* 排序
*
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop() public sort?: string;
/**
* 行为组
*
* @type {Array<any>}
* @memberof AppPickerMicroservice
*/
@Prop() public actionDetails?: Array<any>;
/**
* 值
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public value?: any;
/**
* 占位提示
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public placeholder?: string;
/**
* 值分割符
*
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop({ default: ',' }) public valueSeparator!: string;
/**
* @description 数据展示模式
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop({ default: 'list' }) public mode!: string;
/**
* @description 是否多选
* @type {boolean}
* @memberof AppPickerMicroservice
*/
@Prop({ default: false }) public multiple!: boolean;
/**
* @description 数据查询地址
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop() public url?: string;
/**
* @description 字节的过滤键
* @type {string}
* @memberof AppPickerMicroservice
*/
@Prop({ default: 'pvalue' }) public leafFilterKey!: string;
/**
* @description 是否搜索查询
* @type {boolean}
* @memberof AppPickerMicroservice
*/
@Prop({ default: false }) public enableSearch!: boolean;
/**
* @description 是否只允许选中子节点
* @type {boolean}
* @memberof AppPickerMicroservice
*/
@Prop({ default: false }) public onlyLeaf!: boolean;
/**
* @description 重置项名称
* @type {boolean}
* @memberof AppPickerMicroservice
*/
@Prop() public resetItemName?: string;
/**
* @description 填充对象
* @type {*}
* @memberof AppPickerMicroservice
*/
@Prop() public fillMap?: any;
/**
* 表单状态对象
*
* @type {Subject<any>}
* @memberof AppEmbedPicker
*/
@Prop() public formState!: Subject<any>
/**
* 订阅对象
*
* @protected
* @type {(Subscription | undefined)}
* @memberof SelectType
*/
protected formStateEvent: Subscription | undefined;
/**
* @description 显示值label
* @type {*}
* @memberof AppPickerMicroservice
*/
showLabel: string = 'label';
/**
* @description 显示值value
* @type {*}
* @memberof AppPickerMicroservice
*/
showValue: string = 'id';
/**
* @description 数据
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public items: any[] = [];
/**
* @description 树结构数据
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public treeData: any[] = [];
/**
* @description 树结构默认参数
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public defaultProps: any = { label: 'label' };
/**
* @description 树默认选中数据
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public defaultKeys: string[] = [];
/**
* @description 选中数据map
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public selectionMap: Map<string, any> = new Map();
/**
* @description 显示文本
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public text: string = '';
/**
* @description 下拉类名
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public popperClass: string = 'app-picker-microservice__popper';
/**
* @description 唯一标识
* @type {any[]}
* @memberof AppPickerMicroservice
*/
public uuid: string = Util.createUUID();
get curvalue() {
if (this.value && this.valueitem) {
return this.value;
}
return this.text;
}
set curvalue(value: string) {
if (this.enableSearch && this.value) {
this.handleEmit('', '');
}
this.text = value;
}
@Watch('data',{deep: true})
onDataChange(newData: any, oldData: any) {
if (newData && oldData && this.resetItemName && Object.is(newData[this.resetItemName], oldData[this.resetItemName])) {
this.items = [];
}
}
/**
* @description 生命周期
* @memberof AppPickerMicroservice
*/
async created() {
if(this.formState) {
this.formStateEvent = this.formState.subscribe(({ type, data }) => {
if (Object.is('load', type) && !this.valueitem) {
this.load();
if (this.items.length > 0) {
const item = this.items.find(item => Object.is(item[this.showValue], this.value));
if (item) {
this.text = item[this.showLabel];
}
}
}
});
}
}
/**
* @description 生命周期
* @memberof AppPickerMicroservice
*/
mounted() {
if (this.fillMap) {
this.showLabel = this.fillMap.label;
this.showValue = this.fillMap.id;
this.defaultProps.label = this.fillMap.label;
}
this.popperClass += ` app-picker-microservice__popper-${this.uuid}`;
}
/**
* @description 组件销毁
* @memberof AppPickerMicroservice
*/
destroyed() {
if (this.formStateEvent) {
this.formStateEvent.unsubscribe();
}
}
/**
* @description 搜索数据
* @memberof AppPickerMicroservice
*/
async onSearch(query: string, callback: Function) {
await this.load();
if (Object.is(this.mode, 'tree')) {
callback(this.treeData);
} else {
callback(this.items);
}
this.$nextTick(() => {
this.filter(query, callback);
})
this.togglePopper(true);
}
/**
* @description 数据加载
* @memberof AppPickerMicroservice
*/
async load() {
if (this.url && this.items.length == 0) {
let queryParam: any = {};
if (this.localParam && Object.keys(this.localParam).length > 0) {
queryParam = this.$util.computedNavData(this.data, this.context, this.viewparams, this.localParam);
}
try {
const response = await this.$http.post(this.url, queryParam);
if (response && response.status !== 200) {
this.$throw('数据加载失败', 'onSearch');
}
if (Object.is(this.mode, 'tree')) {
if (response.data && response.data.length > 1) {
this.items = response.data || [];
this.handleLevelCodeList(this.items);
} else {
this.treeData = response.data || [];
this.items = [];
this.transformTreeData(this.treeData);
}
} else {
this.items = response.data || [];
}
this.setDefaultSelect();
} catch (error) {
this.$throw(error, 'onSearch');
}
}
}
/**
* @description 数据过滤
* @memberof AppPickerMicroservice
*/
filter(query: string, callback: Function) {
if (this.enableSearch && !this.multiple) {
if (Object.is(this.mode, 'tree')) {
const tree: any = this.$refs.tree;
if (tree) {
tree.filter(query);
callback(this.treeData);
}
} else {
const results = query
? this.items.filter(restaurant => {
return restaurant[this.showLabel].toLowerCase().indexOf(query.toLowerCase()) === 0;
})
: this.items;
callback(results);
}
}
}
/**
* @description 数据选中
* @memberof AppPickerMicroservice
*/
onSelect(item: any) {
if (this.multiple) {
if (this.selectionMap.has(item[this.showValue])) {
this.selectionMap.delete(item[this.showValue]);
} else {
this.selectionMap.set(item[this.showValue], item);
}
this.valueChang();
} else {
this.handleEmit(item[this.showLabel], item[this.showValue]);
}
}
/**
* @description 树数据选中
* @memberof AppPickerMicroservice
*/
onTreeSelect(item: any, node: any) {
if (this.onlyLeaf && item.children) {
return;
}
if (this.multiple) {
if (this.selectionMap.has(item[this.showValue])) {
this.selectionMap.delete(item[this.showValue]);
} else {
this.selectionMap.set(item[this.showValue], item);
}
this.valueChang();
} else {
this.handleEmit(item[this.showLabel], item[this.showValue]);
}
this.togglePopper();
}
/**
* @description 切换下拉显示状态
* @memberof AppPickerMicroservice
*/
togglePopper(show: boolean = false) {
const popper: any = document.querySelector(`.app-picker-microservice__popper-${this.uuid}`);
if (popper) {
popper.style.display = show ? 'block' : 'none';
}
}
/**
* @description 清空
* @memberof AppPickerMicroservice
*/
onClear() {
this.handleEmit('', '');
this.text = '';
const tree: any = this.$refs.tree;
if (tree) {
tree.filter('');
}
}
/**
* @description 设置默认选中
* @memberof AppPickerMicroservice
*/
setDefaultSelect() {
if (this.items.length > 0 && this.value) {
const keys: string[] = this.value.split(this.valueSeparator);
keys.forEach((key: string) => {
const data = this.items.find(item => Object.is(item[this.showValue], key));
if (data) {
this.selectionMap.set(key, data);
}
});
this.defaultKeys = keys;
}
}
/**
* @description 树过滤节点
* @memberof AppPickerMicroservice
*/
filterNode(value: string, data: any) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
}
/**
* @description 数据变更
* @memberof AppPickerMicroservice
*/
valueChang() {
let value = '';
let label = '';
this.selectionMap.forEach((item: any) => {
value += this.valueSeparator + item[this.showValue];
label += this.valueSeparator + item[this.showLabel];
});
if (value.length > 0) {
value = value.slice(1);
label = label.slice(1);
}
this.handleEmit(label.slice(1), value.slice(1));
}
/**
* @description 处理抛值
* @memberof AppPickerMicroservice
*/
handleEmit(label: string, value: string) {
this.text = label;
if (this.valueitem) {
this.$emit('formitemvaluechange', {
name: this.name,
value: label,
});
this.$emit('formitemvaluechange', {
name: this.valueitem,
value: value,
});
} else {
this.$emit('formitemvaluechange', {
name: this.name,
value: value,
});
}
}
/**
* 转换树数据为平铺数据
*
* @param {*} items
* @memberof AppPickerMicroservice
*/
transformTreeData(treeData: any[]) {
if (treeData.length > 0) {
treeData.forEach((node: any) => {
this.items.push(node);
if (node.children && node.children.length > 0) {
this.transformTreeData(node.children);
}
});
}
}
/**
* 处理层级代码表
*
* @param {*} items
* @memberof AppPickerMicroservice
*/
public handleLevelCodeList(items: Array<any>) {
if (items && items.length > 0) {
const hasChildren = items.some((item: any) => {
return item[this.leafFilterKey];
});
if (hasChildren) {
let list: Array<any> = [];
items.forEach((codeItem: any) => {
if (!codeItem[this.leafFilterKey]) {
let valueField: string = codeItem[this.showValue];
this.setChildCodeItems(valueField, items, codeItem);
list.push(codeItem);
}
});
this.treeData = list;
}
}
}
/**
* 计算子类代码表
*
* @param {*} items
* @memberof AppPickerMicroservice
*/
public setChildCodeItems(pValue: string, result: Array<any>, codeItem: any) {
result.forEach((item: any) => {
if (item[this.leafFilterKey] && item[this.leafFilterKey] == pValue) {
let valueField: string = item[this.showValue];
this.setChildCodeItems(valueField, result, item);
if (!codeItem.children) {
codeItem.children = [];
}
codeItem.children.push(item);
}
});
}
}
</script>