首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rxjs angular 2:包含多个术语的搜索过滤器

rxjs angular 2:包含多个术语的搜索过滤器
EN

Stack Overflow用户
提问于 2017-06-19 16:31:29
回答 2查看 2.1K关注 0票数 0

我正在尝试实现多个术语的搜索。N chart flow of want i tried to implement

我读到了这个博客:https://netbasal.com/manage-your-filters-like-a-pro-in-angular-with-combinelatest-e7b0204be2df it相似的案例。但我不明白如何使用动态观察值(搜索词输入)来实现它。假设我有30个搜索词的过滤器。简单的解决方案是使用combineLatest到所有的搜索词组件,但效率不高。我的目标是在第一次更改时订阅可观察对象。

我尝试对动态数组使用combineLatest运算符,但它不起作用:(。

我的代码:

代码语言:javascript
复制
import {Component, OnInit, Input, ViewChildren, QueryList, AfterViewInit} from '@angular/core';
import {Table} from "../../models/table";
import {InputFilterComponent} from "../types/input-filter/input-filter.component";
import {Observable} from "rxjs/Observable";

@Component({
  selector: '[phantom-thead-filter]',
  template: '<th>Actions</th>
  <th *ngFor="let col of cols" class="ng2-smart-th {{ col.title }}" [ngClass]="col?.class">
    <input-filter [col]="col" [table]="table"></input-filter>
  </th>
  ',
  styleUrls: ['./phantom-thead-filter.component.css']
})
export class PhantomTheadFilterComponent implements OnInit, AfterViewInit {
  @Input() cols;
  @Input() table: Table;
  @ViewChildren(InputFilterComponent) filters : QueryList<InputFilterComponent>;

  constructor() { }

  ngOnInit() {
  }

  ngAfterViewInit() {
    const filters = this.filters.map(f => f.getFilterWatcher());
    Observable.combineLatest(filters)
      .map(( filters : any[] ) => {
        filters.map((filter) => {
          return filter;
        });
      });
  }

}




import {Component, Input, OnInit} from '@angular/core';
import {FormControl} from "@angular/forms";
import {Table} from "../../../models/table";

@Component({
  selector: 'input-filter',
  template: '<input [formControl]="inputControl"
                    class="form-control"
                    type="text"
                    placeholder="input" />',
  styleUrls: ['./input-filter.component.css']
})
export class InputFilterComponent implements OnInit {
  inputControl = new FormControl();
  @Input() col;
  @Input() table: Table;
  changeFilter: any;
  constructor() { }

  ngOnInit() {
    this.changeFilter =this.inputControl.valueChanges
      .skip(1)
      .distinctUntilChanged()
      .debounceTime(400)
      .map((value: string) => {
        return value;
      }).startWith(null);
  }

  getFilterWatcher() {
    return this.changeFilter;
  }

  ngOnDestroy() {
    this.changeFilter.unsubscribe();
  }

}

EN

回答 2

Stack Overflow用户

发布于 2017-06-19 17:03:45

CombineLatest等待所有可观测对象发出它们的第一个值。

如果即使没有设置所有过滤器也要查询,可以使用startWith提供一个虚拟的第一个null值。

代码语言:javascript
复制
const filters = this.filters.map(f => f.changeFilter.startWith(null));

Observable.combineLatest(...filters)
          .map(( filters : ActiveFilter[] ) => {
                // All filters are defined, but not all have values [null, 'search', null, null]
          });
票数 0
EN

Stack Overflow用户

发布于 2017-06-19 17:15:39

您可以尝试使用此方法:

代码语言:javascript
复制
let listObs = [];
const searchTermA = Observable.fromEvent(this.searchInputARef.nativeElement, 'keyup').debounceTime(300).distinctUntilChanged();
listObs = [...listObs, searchTermA];

const searchTermB = Observable.fromEvent(this.searchInputBRef.nativeElement, 'keyup').debounceTime(300).distinctUntilChanged();
listObs = [...listObs, searchTermB];

Observable.combineLatest(list).subscribe(latestValues => {
    const [searchTermAValue, searchTermBValue] = latestValues;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44625667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档