如何在客户端过滤 FirebaseListObservable?

How to filter FirebaseListObservable on client side?

我在 Angular 网络应用程序中使用 AngularFire2。由于 Firebase 的查询限制,我无法形成一个能够准确提供我需要的数据的查询(至少在不对我的模式进行重大更改的情况下)。

所以我想在 javascript(打字稿)中应用一个额外的客户端过滤条件。我该怎么做呢?我可以以某种方式向可观察对象添加过滤功能吗?下面是说明我在做什么的片段。

在组件的 HTML 模板中,我有类似下面的内容。 html 片段中的 "jobs" 变量是一个 FirebaseListObservable。

<tr *ngFor="let job of jobs | async"> 
  .. fill in the table rows

组件代码如下所示:

   // in the member declaration of the class
   jobs : FirebaseListObservable<Job[]>;

   ...
   // Notice in ngOnInit(), I'm filtering the jobs list using the the
   // Firebase criteria. But I want to filter on an additional field. 
   // Firebase allows only single field criteria, so I'm looking for 
   // a way to apply an additional client-side filter to jobs to eliminate
   // some additional records. 

   ngOnInit(){
      this.jobs = this.af.database.list("/jobs/", {query: {orderByChild : "companyKey", equalTo:someKey}})
   }

有没有办法在 "this.jobs" 上应用过滤器组件,以便我可以应用本地(客户端)过滤器?

我还没有找到真正过滤 observable 的方法,但我找到了某些情况下的解决方法。可以使用 *ngFor 在元素中进行伪过滤。所以我的例子变成了

<tr *ngFor="let job of jobs | async" [hidden]="filter(job)"> 
  .. fill in the table rows

添加我们将 filter() 方法添加到我们的组件代码中:

// in the member declaration of the class
   jobs : FirebaseListObservable<Job[]>;

   ...

   ngOnInit(){
      this.jobs = this.af.database.list("/jobs/", {query: {orderByChild : "companyKey", equalTo:someKey}})
   }

   filter(job : Job) : boolean{
     // Return true if don't want this job in the results.
     // e.g. lets filter jobs with price < 25;
     if (job.price < 25){
       return true;
     }
     return false; 
  }

我仍在寻找一种真正过滤 observable 的方法,但与此同时,这个变通方法是可用的。

我运行进入同样的问题。缺少的部分是过滤数组本身(Job[]),而不是它周围的 Observable 包装器(FirebaseListObservable<Job[]>)。

能够使用 RxJS 运算符做到这一点 map

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map'

...

@Component({
  ...
})
export class JobComponent {

  ...

  getJobsLessThanPrice(price: number): Observable<Job[]> {
    return this.af.database.list('jobs', {query: {...}})
      .map(_jobs => _jobs.filter(job => job.price > price));

  }
}

如果job.price > price returns为真,则包含。

还注意到你的 jobs 属性 是 FirebaseListObservable<Job> 类型,我本以为可观察类型应该是 Job[] vs Job.

你可以写你自己的pipe (类似于 Filter on Angular 1.X)

import {Pipe, PipeTransform} from '@angular/core';
import * as _ from 'lodash';

@Pipe({
  name: 'search'
})
export class SearchPipe implements PipeTransform {

 transform(value: any, args?: any): any {

  if (args) {
    return _.filter(value, d => _.find(_.valuesIn(d), v => 
        _.toLower(v).indexOf(_.toLower(args)) !== -1));
  }

 return value;
 }
 }   

然后,在组件模板中使用此管道:

<input [(ngModel)]="term"> 
<li *ngFor="let item of items | async | search:term">{{item}}</li>