import { Component, OnInit, Input, Output, EventEmitter, SimpleChanges } from '@angular/core'; import { RhSelectableDto, RhSafeAny } from 'rh-base/model'; import { ArrayHelper, ObjectHelper } from 'rh-base/core'; @Component({ selector: 'rh-checkable-list', templateUrl: './checkable-list.component.html', styleUrls: ['./checkable-list.component.less'] }) export class CheckableListComponent implements OnInit { @Input() rhData: RhSelectableDto[] = []; @Input() rhDataLoading = false; @Input() rhName!: string; @Input() rhDisplayName!: string; @Input() rhSelectedData: RhSelectableDto[] | null = null; @Output() rhSelectedDataChange = new EventEmitter(); tempDataset: RhSelectableDto[] = []; searchString = ''; dataset: RhSelectableDto[] = []; isAllCheck = false; isIndeterminate = false; ngOnInit() { if (!this.rhSelectedData) { this.rhSelectedData = this.dataset.filter((ele) => ele.select); } this.tempDataset = [...this.rhSelectedData]; // this.tempDataset = _.cloneDeep(this.rhSelectedData); } ngOnChanges(changes: SimpleChanges) { const { rhData } = changes; if (rhData && rhData.currentValue) { this.isAllCheck = rhData.currentValue.every((ele: RhSafeAny) => ele.select); this.isIndeterminate = rhData.currentValue.some((ele: RhSafeAny) => ele.select) && !this.isAllCheck; this.dataset = [...rhData.currentValue]; } } ngModelChange(item: RhSelectableDto) { if (!item) { return; } if (item.select) { this.tempDataset = ArrayHelper.AddTo(this.tempDataset, item, (ele) => ele[this.rhName] === item[this.rhName], false); if (this.tempDataset.length === this.rhData.length) { this.isAllCheck = true; } } else { this.tempDataset = ArrayHelper.Remove(this.tempDataset, (ele) => ele[this.rhName] === item[this.rhName]); this.isAllCheck = false; } this.isIndeterminate = this.dataset.some((ele) => ele.select) && !this.isAllCheck; this.rhSelectedDataChange.emit(this.tempDataset); } submit() { // 先置空 this.dataset = []; if (this.searchString.trim() !== '') { // this.rhData.forEach((item) => { // // tslint:disable-next-line:max-line-length // if ( // this.filterOption( // this.searchString, // item[this.rhName] + item[this.rhDisplayName] + item['OrganizationDisplayName'] ? item['OrganizationDisplayName'] : '' // ) // + item['OrganizationDisplayName'] ? item['OrganizationDisplayName'] : '' // ) { // this.dataset.push(item); // } // }); this.dataset = this.rhData.filter((item) => { return this.filterOption( this.searchString, item[this.rhName] + item[this.rhDisplayName] + (item['OrganizationDisplayName'] ? item['OrganizationDisplayName'] : '') ); }); } else { this.dataset = [...this.rhData]; } } setSearchString(ele: string) { // console.log(ele); this.searchString = ele; } allCheckChange(status: boolean) { this.dataset.forEach((ele) => (ele.select = status)); // this.ngModelChange(null); if (status) { this.tempDataset = []; //先置空 this.tempDataset = ObjectHelper.cloneDeep(this.rhData); } else { this.tempDataset = []; } this.rhSelectedDataChange.emit(this.tempDataset); } // 前端搜索 private filterOption(inputValue: string, item: RhSafeAny): boolean { return item.indexOf(inputValue) > -1; } }