import { DataTableComponent } from '@farris/ui-datatable'; import { of } from 'rxjs'; import { catchError, take, tap } from 'rxjs/operators'; import { FavoriteAction, FAVORITE_FIELD_NAME } from '../lookup-displaytype'; import { LookupGridResult } from '../lookup-grid-options'; import { LookupGridComponent } from '../lookup-grid.component'; import { LookupSelectionService } from './lookup-selection.service'; export class DataTableEventManager { private lookupSelectionSer: LookupSelectionService; constructor(private ins: LookupGridComponent) { this.lookupSelectionSer = this.ins.lookupSelectionSer; } onSearch($event: { field: string; value: string } = { field: '*', value: '' }) { if ($event && $event.field !== '*' && !$event.value) { this.ins.messagerService.warning(this.ins.mustWriteSomething); return; } const p = { pageInfo: { pageIndex: 1, pageSize: this.ins.gridOptions.pageSize }, search: $event }; if (this.ins.uri) { if (!this.ins.searching) { this.ins.searching = true; this.ins.httpMgr.getData(p, 'list').pipe( catchError(err => { this.ins.searching = false; return of({ "_ERROR_": err }); }), tap(() => { this.ins.searching = false; }) ).subscribe( data => { this.ins.searching = false; this.ins.closeLoading(); if (!data['_ERROR_']) { this._loadData(data); } else { throw new Error(data['_ERROR_']); } } ); } } else { this.ins.search.emit(p); } } private _loadData(data: LookupGridResult) { const self = this.ins; self.closeLoading(); self.favHelper.updateFavoritesStatus(data.items); self.loadDataTableData(data); // 选中数据 this.ins.selectionMgr.selectCurrentValue(); } bindDataTableEvent() { const self = this.ins; const dt = self.componentRef.instance as DataTableComponent; dt.selectedRow.subscribe((e: any) => { if (this.ins.singleSelect) { this.lookupSelectionSer.clearSelections(); } this.ins.checkedChange.emit({ data: [e.data], isCheck: true }); this.lookupSelectionSer.updateSelections([e.data]); dt.cd.detectChanges(); }); dt.unSelectRow.subscribe(e => { this.lookupSelectionSer.unSelect(e.data[self.idField]); this.ins.checkedChange.emit({ data: [e.data], isCheck: false }); }); dt.checkAll.subscribe((e: boolean) => { this.lookupSelectionSer.updateSelections(dt.data, e); this.ins.checkedChange.emit({ data: dt.data, isCheck: e }); }); dt.pageChanged.subscribe((e: any) => { if (self.uri) { self.httpMgr.getData(e, 'list').subscribe((data: LookupGridResult) => { this._loadData(data); }); } else { self.pagerChanged.emit(self.httpMgr.buildQueryParams(e, 'list')); } }); dt.pageSizeChanged.subscribe(e => { if (self.uri) { self.httpMgr.getData(e, 'list').subscribe( data => { this._loadData(data); }, err => { self.closeLoading(); } ); } else { self.pagerChanged.emit(self.httpMgr.buildQueryParams(e, 'list')); } }); dt.search.subscribe((e: any) => { self._searchState = e; this.onSearch(e); }); // 双击事件 dt.rowDblClick.subscribe((rowData: any) => { if (self.gridOptions.singleSelect) { // this.lookupSelectionSer.updateSelections([rowData]); self.selectItem(rowData); } }); // 收藏事件 dt.cellClick.subscribe((e: any) => { if (e.col.field === FAVORITE_FIELD_NAME) { e.event.stopPropagation(); const classList = e.event.target['classList']; if (classList.contains('f-lookup-favorite')) { self.items.forEach(item => { const id = self.utils.getValue(self.idField, item); if (id === self.utils.getValue(self.idField, e.row)) { item[FAVORITE_FIELD_NAME] = !item[FAVORITE_FIELD_NAME]; } }); dt.loadData({ pageSize: self.gridOptions.pageSize, pageIndex: self.gridOptions.pageIndex, total: self.gridOptions.total, data: self.gridOptions.items }); // 更新收藏数据 const faction = e.row[FAVORITE_FIELD_NAME] ? FavoriteAction.add : FavoriteAction.delete; if (faction === FavoriteAction.add) { this.ins.favoriteItems = [...this.ins.favoriteItems, e.row]; } else { this.ins.favoriteItems = this.ins.favoriteItems.filter(n => { return self.utils.getValue(self.idField, n) !== self.utils.getValue(self.idField, e.row); }); } this.lookupSelectionSer.updateFavoriteData(e.row, faction); } } }); dt.columnSorted.subscribe((sort: { sortName: string; sortOrder: any }) => { if (!this.ins.remoteSort) { return; } const { sortName, sortOrder } = { ...sort }; const col = this.ins.columns.find(n => n.field === sortName); const _sortName = col ? col.fieldPath ? col.fieldPath : col.field : sortName; const param = { sortName: _sortName, sortOrder, search: self._searchState, pageInfo: { pageSize: self.pageSize, pageIndex: 1 } }; self.httpMgr.getData(param, 'search').subscribe(d => { self.loadDataTableData(d); self.closeLoading(); }); }); dt.clearSearchValue.subscribe(() => { self._searchState = null; this.onSearch(); }); } }