How to filter a mat-tree component Angular Material 6.0.1

前端 未结 4 628
醉酒成梦
醉酒成梦 2020-12-28 08:47

I\'m using mat-tree angular material component. It\'s a nice component with some very useful features like, multi-select, expand all/collapse all. I was not able to find any

4条回答
  •  盖世英雄少女心
    2020-12-28 09:15

    First add an input as the filter in the view. Bind keyup event to rxjs Subject

    
    

    Then query your backend to filter the tree node with keyword

    this.keyEvent.pipe(
      map((e: any) => e.target.value.toLowerCase()),
      debounceTime(500),
      distinctUntilChanged(),
      switchMap((keyword: string) => {
        if (keyword && keyword.length > 2) {
          return this.yourservice.searchForData(this.entId, keyword);
        } else {
          return of();
        }
      })
    )
    .subscribe((r) => {
      this.nestedDataSource.data = r;
      this.nestedTreeControl.dataNodes = r;
      this.nestedTreeControl.expandAll();
    });
    

提交回复
热议问题