import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { Subject } from 'rxjs' ; import { debounceTime } from 'rxjs/operators' ; /**
 * 异步下拉框数据源
 */ export class SelectAsyncDs { options = [ ] ; pageNum = 1 ; loading = false ; allDataLoaded = false ; query = '' ; pageSize = 15 ; getPaggingListFunc : ( pageNum , pageSize , query ) => Observable < any > ; doSearch$ = new Subject < any > ( ) ; constructor ( getPaggingListFunc ) { this . getPaggingListFunc = getPaggingListFunc ; this . doSearch$ . pipe ( debounceTime ( 300 ) ) . subscribe ( ( v ) => { this . query = v || '' ; this . options = [ ] ; this . pageNum = 1 ; this . reload ( ) . subscribe ( ) ; } ) ; } onSearch = ( v ) => { this . doSearch$ . next ( v ) ; } ; reload ( ) { this . loading = true ; return this . getPaggingListFunc ( this . pageNum , this . pageSize , this . query ) . pipe ( map ( ( data ) => { this . loading = false ; this . options = data ; return this . options ; } ) ) ; } loadMore ( ) { if ( this . loading ) { return ; } if ( this . allDataLoaded ) { return ; } this . pageNum += 1 ; this . loading = true ; return this . getPaggingListFunc ( this . pageNum , this . pageSize , this . query ) . subscribe ( ( data ) => { if ( ! data || data . length <= 0 ) { this . allDataLoaded = true ; } this . loading = false ; this . options = [ ... this . options , ... data ] ; } ) ; } appendOption ( dto , getIdFunc ) { this . options = [ dto , ... this . options . filter ( ( t ) => getIdFunc ( t ) !== getIdFunc ( dto ) ) ] ; } }