import { Component, OnInit, Input, Output, EventEmitter, SimpleChanges } from '@angular/core'; import { RhSafeAny, RhSelectableDto } from 'rh-base/model'; @Component({ selector: 'rh-item-selector-modal', templateUrl: './item-selector-modal.component.html', styleUrls: ['./item-selector-modal.component.less'] }) export class RhItemSelectorModalComponent implements OnInit { @Input() rhIsDataString = false; @Input() rhData!: RhSelectableDto[]; /** 在对话框页面展示的中文名称 */ @Input() rhDisplayName = ''; displayNameList: string[] = []; /** 是否启用前端搜索 */ @Input() rhSearch = true; /** 数据源模式 */ @Input() rhMode: 'object' | 'string' = 'object'; // tslint:disable-next-line: variable-name private _rhVisiable = false; @Input() get rhVisiable() { return this._rhVisiable; } set rhVisiable(value: boolean) { this._rhVisiable = value; this.rhVisiableChange.emit(value); } @Output() rhVisiableChange = new EventEmitter(); @Input() rhTitle = '数据编辑'; @Input() rhWidth:string|number = 640; // @Output() nzOnCancel = new EventEmitter(); @Output() rhSelectedChange = new EventEmitter(); @Output() rhClose = new EventEmitter(); displayDataset!: RhSelectableDto[]; inputValue = ''; timer: RhSafeAny; constructor() { // } ngOnInit() { this.displayNameSplit(); this.rhData = this.rhData || []; if (this.rhMode === 'object') { this.displayDataset = this.datasetArrayHandler(this.rhData); } else if (this.rhMode === 'string') { this.displayDataset = this.datasetArrayHandler(this.rhData); } } ngOnChanges(changes: SimpleChanges) { const { rhData } = changes; if (rhData?.currentValue && !rhData.firstChange) { this.displayDataset = this.datasetArrayHandler(rhData.currentValue) || []; } } displayNameSplit() { const names = this.rhDisplayName.split('-'); names.forEach((ele) => { this.displayNameList.push(ele); }); } onSelectItem(item: RhSafeAny) { this.rhSelectedChange.emit(item); this.closeModal(); } closeModal() { this.rhClose.emit(); } onInputValueChange(value: string) { if (this.timer) { clearTimeout(this.timer); } if (!value || !value.trim()) { this.displayDataset = this.datasetArrayHandler(this.rhData); } else { this.timer = setTimeout(() => { this.searchData(value.trim()); }, 500); } } private searchData(filterValue: string) { const temp: RhSelectableDto[] = []; this.rhData.forEach((ele) => { // 同时过滤Text和Value if (this.rhMode === 'object') { if (this.displayNameList.length > 1) { if ( (ele[this.displayNameList[0]] as string).toLocaleLowerCase().indexOf(filterValue.toLocaleLowerCase()) !== -1 || (ele[this.displayNameList[1]] as string).toLocaleLowerCase().indexOf(filterValue.toLocaleLowerCase()) !== -1 ) { temp.push(ele); } } else { if ((ele[this.displayNameList[0]] as string).toLocaleLowerCase().indexOf(filterValue.toLocaleLowerCase()) !== -1) { temp.push(ele); } } } else { if ((ele as RhSafeAny).toLocaleLowerCase().indexOf(filterValue.toLocaleLowerCase()) !== -1) { temp.push(ele); } } }); if (this.rhMode === 'object') { this.displayDataset = temp; } else { this.displayDataset = this.datasetArrayHandler(temp); } } clearInputValue() { this.inputValue = ''; this.displayDataset = this.datasetArrayHandler(this.rhData); } datasetArrayHandler(datas: RhSelectableDto[] | string[]): RhSelectableDto[] { datas = datas || []; if (this.rhMode === 'string') { return (datas as string[]).map((ele) => { return { name: ele, displayName: ele }; }); } else { return [...datas] as RhSelectableDto[]; } } }