简体   繁体   English

从Firestore Whitanglefire过滤数据

[英]Filter data from Firestore whit angularfire

I'm trying to filter data from Firestore with Angularfire's Querying Collection: https://github.com/angular/angularfire2/blob/master/docs/firestore/querying-collections.md I'm a bit confused with the approach. 我正在尝试使用Angularfire的查询集合从Firestore过滤数据: https : //github.com/angular/angularfire2/blob/master/docs/firestore/querying-collections.md我对此方法有些困惑。 I have managed to filter through a button but I do not know how to reset or remove that filter once done. 我已经设法通过按钮进行过滤,但是一旦完成,我不知道如何重置或删除该过滤器。

What is the correct way to filter and reset said filter? 过滤和重置所述过滤器的正确方法是什么?

Here I have a stackblitz: https://stackblitz.com/edit/query-collections-in-angularfire2 在这里我有一个stackblitz: https ://stackblitz.com/edit/query-collections-in-angularfire2

My code so far: 到目前为止,我的代码:

service.ts 服务

filterBy() {
  this.avisos = this.afs.collection('avisos', ref => ref.where('categoria','==', 'Vehículos' )).valueChanges()
  return this.avisos;
};

myComponent.ts myComponent.ts

filtrarData() {
  this.avisos = this.fs.filterBy()
  return this.avisos;
};

myComponent.html myComponent.html

<button (click)="filtrarData()" class="btn btn-outline-primary">Vehículos</button>

In your firebase service, there are two methods, one to get the collection with no filters and one to get the collection filtered by a given 'categoria'. 在您的Firebase服务中,有两种方法,一种是获取没有过滤器的集合,另一种是通过给定的“类别”对集合进行过滤。

In your component, when gets initialized ( ngOnInit() function) you call the method from the service to retrieve the whole collection; 在组件中,当初始化时( ngOnInit()函数),您从服务中调用该方法以检索整个集合; and then the button 'Vehiculos' triggers the function filtrarData() with the given 'categoria' from your service. 然后按钮“ Vehiculos”会从您的服务中使用给定的“类别”触发函数filtrarData()

After it has been filtered, if you want to "clean" the filter and get the whole collection back, it would be as easy as having a button calling the same as ngOnInit() is doing and then your array this.avisos would have again the whole collection: 过滤之后,如果您想“清理”过滤器并取回整个集合,就像按钮调用与ngOnInit()一样ngOnInit() ,然后您的数组this.avisos将再次this.avisos整个收藏:

app.component.ts app.component.ts

  ngOnInit() {
    this.getAvisos()
  }

  getAvisos() {
      this.avisos = this.fs.getDomiciliarios()
  }

  filtrarData() {
      this.avisos = this.fs.filterBy()
  }

then in the html of the component app.component.html : 然后在组件app.component.html的html中:

<button (click)="getAvisos()" class="btn btn-outline-primary btn-sm mx-1">Clean Filters</button>
<span>Filter by:</span>
<button (click)="filtrarData()" class="btn btn-outline-primary btn-sm mx-1">Vehículos</button>
<hr>

By the way, notice that there is no need to do the return this.avisos line in the app.component.ts, that would only be needed for the service. 顺便说一句,请注意,不需要执行return this.avisos中的this.avisos行,这仅是服务所需要的。

Additionally, would be good if you standardise a bit the firebase.service function to get the collection filtered by 'categoria' with an argument, in case you want to call it for more than one type of 'categoria': 另外,如果您要对firebase.service函数进行一些标准化以使收集的“ categoria”带有一个参数,则可能会很好,以防您要为一种以上的“ categoria”类型调用它:

firebase.service.ts firebase.service.ts

filterBy(categoriaToFilter: string) {
    this.avisos = this.afs.collection('avisos', ref => ref.where('categoria','==', categoriaToFilter )).valueChanges()

    return this.avisos;
};

so now in your app.component.html you could have something like: 所以现在在您的app.component.html中,您可以看到类似以下内容的内容:

<button (click)="filtrarData('Vehículos')" class="btn btn-outline-primary btn-sm mx-1">Vehículos</button>

<button (click)="filtrarData('Casa')" class="btn btn-outline-primary btn-sm mx-1">Casa</button>

but take into account that your component app.component.ts would then include: 但请考虑到您的组件app.component.ts将包括:

filtrarData(categoriaToFilter: string) {
    this.avisos = this.fs.filterBy(categoriaToFilter)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM