如何使用变量制作firebase过滤器?

问题描述 投票:0回答:2

我想从我的Angular firebase数据库中过滤一些东西。为此,我从div中读出了文本。这给了我应该过滤我的数据库的文本。我将此值传输到我的服务,我想将其用作变量并过滤此变量,但这不起作用。

这是我的服务:

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);
  }

函数“filtere()”改变变量“filterWert”的值。这工作正常,因为当我在console.log它时,我得到正确的值,但cvCollectionFilter内的值不会改变,我不知道为什么。

这是要过滤的属性元素的component.ts文件:

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

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

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

这是应该过滤的元素的component.ts文件:

SKILLS: IntCvSkill[];

  constructor(private skillService: CvServiceService) { }

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

以下是要过滤的属性元素的component.html文件:

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

这是应该过滤的元素的component.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>

奇怪的是,当我在代码中设置我想要通过mySelf过滤的值时,它可以工作。如何在cvSkillCollectionFilter中更改filterWert变量的值?

更新:问题是,在构造函数中我无法更新变量filterWert。我现在正在搜索如何制作它的解决方案。

javascript angular firebase google-cloud-firestore angular7
2个回答
1
投票

它在文档中描述:Dynamic querying

使用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);
}

0
投票

我用管道解决了问题。

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

所以我可以在* ngFor中直接过滤。要更改值,只需将文本作为变量发送到变量“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>
© www.soinside.com 2019 - 2024. All rights reserved.