【问题标题】:Use switchMap with Kedno UI Gid in Angular2+在 Angular 2 中使用带有 Kendo UI Grid 的 switchMap
【发布时间】:2018-09-12 15:52:25
【问题描述】:

我将 Kendo UI 用于带有数据绑定的 Angular Grid。我有两种服务:一种是从过滤器组件向网格组件发出搜索事件,另一种是(基于 BehaviorSubject)调用 API。

我使用的与 Grid 进行数据绑定的方式是基于 Kendo 示例。问题是当用户在过滤器输入中输入文本时如何使用 switchMap 来取消 API 请求。我知道 switchMap 运算符,但我不知道如何在基于 Kendo UI Grid 的服务中使用它。我尝试了很多方法都没有成功,我没有更多的想法。我的代码如下:

Grid 组件中使用的 API 服务方法

public query(state: any): void {  
    this.getItems(state)
        .subscribe(x =>   
            super.next(x));  
}  

private getItems(state: any): Observable<GridDataResult>{  
    let page = state.skip / state.take + 1;
    let queryStr =         
    `page=` + page + 
    `&size=` + state.take + 
    `&sortColumn=` + state.sortColumn + 
    `&sortDirection=` + state.sortDirection;

    return this.http  
        .get(`${this.BASE_URL}FindItemsComplex?${queryStr}`)  
        .pipe(
            map(response => (<GridDataResult>{
                data: response['data'],
                total: response['length']
            }))
        );  
}    

网格组件方法

ngOnInit() {
   this.subscription = this.searchService.searchEvent.
   debounceTime(400).
   subscribe((searchModel: ItemsFilter) => 
     {
       this.state = searchModel;   
       this.state.skip = 0;   
       this.state.take = 15;
       this.service.query(this.state);  
     }
   );    
 }

我应该在哪里以及如何使用 switchMap? 感谢您的帮助。

编辑

用于发射事件的过滤器组件方法

HTML

<input kendoTextBox [(ngModel)]="itemsFilter.textSearch" (ngModelChange)="onSearch()" [ngClass]="'form-control'" />

TS

onSearch() {
   this.searchService.Search(this.itemsFilter);
}

搜索服务

export class ItemsSearchService {
   constructor() {
       this.searchEvent = new EventEmitter();
   }

   Search(query :ItemsFilter) {
       this.searchEvent.emit(query);
   }

   searchEvent: EventEmitter<ItemsFilter>;
}

【问题讨论】:

  • 能否包含用户在过滤组件中输入文本时调用的函数?

标签: angular typescript rxjs kendo-ui-angular2 switchmap


【解决方案1】:

当用户在“switchMap”等过滤器输入中键入文本时,函数“debounceTime”也可以取消 API 请求。你已经使用了'debounceTime',所以你不需要再使用'switchMap'了。

【讨论】:

  • 我的经验不同,我想杀死所有以前的请求并让最后一个被调用。但是现在有了“debounceTime”,当我从 API 的请求中获得响应时,我会遇到一些情况,该请求是早些时候调用的,最后一个请求很可能完成得更快。所以输入过滤器的当前状态和表中的数据不匹配
  • debounceTime 将杀死所有先前的请求,并让最后一个请求被您喜欢。请参阅办公站点的以下语句:rxjs-dev.firebaseapp.com/api/operators/debounceTime debounceTime 延迟源 Observable 发出的值,但如果新值到达源 Observable 上,则丢弃先前未决的延迟发射。
  • 查看示例可能会找到答案。 telerik.com/kendo-angular-ui/components/inputs/…
猜你喜欢
  • 2018-03-12
  • 2017-08-14
  • 1970-01-01
  • 2022-01-20
  • 2018-07-29
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多