简体   繁体   English

如何使用变量创建Firebase过滤器?

[英]How can I make a firebase filter with a variable?

I want to filter somethin from my Angular firebase database. 我想从Angular firebase数据库中过滤某物。 For that I read out the text from a div. 为此,我从div中读出了文本。 That gives me the Text that should filter my database. 这给了我应该过滤我的数据库的文本。 I transfer this value to my service, where I want to use it as a variable and filter through this variable, but that doesn't work. 我将此值传输到我的服务,在这里我想将其用作变量并通过该变量进行过滤,但这是行不通的。

Here is my Serice: 这是我的服务:

cvSkillCollectionFilter: AngularFirestoreCollection<IntCvSkill>;
  cvSkillCollectionFilterAnwenden: AngularFirestoreCollection<IntCvSkill>;
  cvSkillFilter: Observable<IntCvSkill[]>;
  cvSkillFilterAnwenden: Observable<IntCvSkill[]>;

  filterWert: string;

  constructor(public afs: AngularFirestore) {

    this.filterWert = 'Entwicklung';

    this.cvSkillCollectionFilter = this.afs.collection('cv-skill', ref => ref.where('skillGruppe', '==', this.filterWert));

    this.cvSkillCollectionFilterAnwenden = this.afs.collection('cv-skill');

    this.cvSkillFilter = this.cvSkillCollectionFilter.snapshotChanges().pipe(map(changes => {
      return changes.map(a => {
        const data = a.payload.doc.data() as IntCvSkill;
        data.id = a.payload.doc.id;
        return data;
      });
    }));

    this.cvSkillFilterAnwenden = this.cvSkillCollectionFilterAnwenden.snapshotChanges().pipe(map(changes => {
      return changes.map(a => {
        const data = a.payload.doc.data() as IntCvSkill;
        data.id = a.payload.doc.id;
        return data;
      });
    }));
  }

  getSkillsGefiltert() {
    return this.cvSkillFilter;
  }

  getSkillsFilterAnwenden() {
    return this.cvSkillFilterAnwenden;
  }

  addItem(skill: IntCvSkill) {
    this.cvSkillCollectionFilter.add(skill);
  }

  filtere(filterElement: string) {
    this.filterWert = filterElement;
    console.log(this.filterWert);
  }

The function "filtere()" changes the value of the variable "filterWert". 函数“ filtere()”更改变量“ filterWert”的值。 This works fine, because when I console.log it, I get the correct value, but the value inside of the cvCollectionFilter doesn't change and I don't know why. 这可以正常工作,因为当我进行console.log记录时,我得到了正确的值,但是cvCollectionFilter内部的值没有变化,我也不知道为什么。

Here is componont.ts file of the elements that are the properties to filter: 这是要过滤的属性的元素的compont.ts文件:

skillGruppen: IntCvSkill[];
  constructor(private skillService: CvServiceService) { }

  ngOnInit() {
    this.skillService.getSkillsFilterAnwenden().subscribe(cvSkillGruppe => {
      this.skillGruppen = cvSkillGruppe;
    });
   }

   filterAnwenden(filterElement: string) {
     this.skillService.filtere(filterElement);
   }

Here is the componont.ts file of the elemts that should get filtered: 这是应该过滤的elemts的componont.ts文件:

SKILLS: IntCvSkill[];

  constructor(private skillService: CvServiceService) { }

  ngOnInit() {
    this.skillService.getSkillsGefiltert().subscribe(cvSkill => {
      this.SKILLS = cvSkill;
    });
  }

Here is componont.html file of the elements that are the properties to filter: 这是要过滤的属性的元素的compont.html文件:

<div>Filter Gruppen:</div>
<div *ngFor="let skillGruppe of skillGruppen">
  <div (click)="filterAnwenden(filter.textContent)" #filter>{{skillGruppe.skillGruppe}}</div>
</div>

Here is the componont.ts file of the elemts that should get filtered: 这是应该过滤的elemts的componont.ts文件:

<div *ngIf="SKILLS?.length > 0; else noItem">
  <div *ngFor="let skill of SKILLS">
    <div class="skill">
      <mat-accordion>
        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>{{skill.skillname}}
            </mat-panel-title>
            <mat-progress-bar class="progress-bar" [value]="skill.skillwert"></mat-progress-bar>
            <mat-panel-description>
            </mat-panel-description>
          </mat-expansion-panel-header>
          <div>{{skill.skillBeschreibung}}</div>
        </mat-expansion-panel>
      </mat-accordion>
    </div>
  </div>
</div>

<ng-template #noItem>
  <div>Keine Skills!</div>
</ng-template>

The strange thing is, when I set the value for that I want to filter by mySelf in the code, it works. 奇怪的是,当我在代码中设置要通过mySelf过滤的值时,它就可以工作。 How can I change the value of the filterWert variable inside the cvSkillCollectionFilter? 如何更改cvSkillCollectionFilter内部的filterWert变量的值?

Update: The problem is, that in the constructor I can't update the variable filterWert. 更新:问题是,在构造函数中我无法更新变量filterWert。 I search now for an solution how to make that. 我现在正在寻找一种解决方案。

It's described in the documentation: Dynamic querying . 在文档中进行了描述: 动态查询

Use a Subject for your filter values and map this Subject to your query. 使用Subject作为您的过滤器值,然后将此主题映射到您的查询。

private filterWert$ = new Subject<string>();

constructor(public afs: AngularFirestore) {
  this.cvSkillFilter = filterWert$.pipe(
    switchMap(filterWert => 
      this.afs.collection('cv-skill', ref => ref.where('skillGruppe', '==', filterWert)).snapshotChanges()
    ),
    map(changes => changes.map(a => {
      const data = a.payload.doc.data() as IntCvSkill;
      data.id = a.payload.doc.id;
      return data;
    }))
  );
}

getSkillsGefiltert() {
  return this.cvSkillFilter;
}

filtere(filterElement: string) {
  this.filterWert$.next(filterElement);
}

I solved the Problem with a pipe. 我用管道解决了问题。

  transform(skills: any, skillgruppe: any): any {
    if (skillgruppe === undefined) {
      return skills;
    }
    return skills.filter(skill => {
      return skill.skillGruppe.toLowerCase().includes(skillgruppe.toLowerCase());
    });
  }

So I can drectly filter in the *ngFor. 因此,我可以直接过滤* ngFor。 To change the value just send the text as a variable to the variable "skillGruppe" 要更改该值,只需将文本作为变量发送到变量“ skillGruppe”

  <div *ngIf="SKILLS?.length > 0; else noItem">
    <div *ngFor="let skill of SKILLS | filter:skillGruppe">
      <div class="skill">
        <mat-accordion>
          <mat-expansion-panel>
            <mat-expansion-panel-header>
              <mat-panel-title>{{skill.skillname}}
              </mat-panel-title>
              <mat-progress-bar class="progress-bar" [value]="skill.skillwert"></mat-progress-bar>
              <mat-panel-description>
              </mat-panel-description>
            </mat-expansion-panel-header>
            <div>{{skill.skillBeschreibung}}</div>
          </mat-expansion-panel>
        </mat-accordion>
      </div>
    </div>
  </div>

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

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