简体   繁体   中英

How can I make a firebase filter with a variable?

I want to filter somethin from my Angular firebase database. For that I read out the text from a 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". 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.

Here is componont.ts file of the elements that are the properties to filter:

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:

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:

<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:

<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. How can I change the value of the filterWert variable inside the cvSkillCollectionFilter?

Update: The problem is, that in the constructor I can't update the variable 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.

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. To change the value just send the text as a variable to the variable "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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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