如何在 ionic 4 firestore 中执行 Search/Filter 列表
How to perform Search/Filter list in ionic 4 firestore
我尝试了很多教程,但我找不到任何关于如何执行搜索的解决方案 function.i 想通过名为 booth 的字段在 StudentList 集合中搜索 如果有人愿意帮助我。所以这基本上是我的编码
firebase.service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument,AngularFirestoreCollection, DocumentReference } from '@angular/fire/firestore';
import { AngularFireAuth } from '@angular/fire/auth';
import { Subscription, Observable } from 'rxjs';
import {map, take} from 'rxjs/operators';
import {studl} from '../modal/studl';
@Injectable({
providedIn: 'root'
})
export class FirebaseService {
private students: Observable<studl[]>;
private studCollection: AngularFirestoreCollection<studl>;
constructor(
public afs: AngularFirestore,
public afAuth: AngularFireAuth) {
this.studCollection = this.afs.collection<studl>('StudentList');
this.students = this.studCollection.snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
});
})
);
}
//getall
getAllStud(): Observable<studl[]> {
return this.students;
}
//getsingle
getStudSingle(id: string): Observable<studl> {
return this.studCollection.doc<studl>(id).valueChanges().pipe(
take(1),
map(note => {
note.id = id;
return note;
})
);
}
}
}
Home.html
<ion-content>
<div class="title-block">
<div class="heading"></div>
</div>
<ion-searchba></ion-searchbar >
<ion-list>
<ion-list-header>
<ion-label>Select your student</ion-label>
</ion-list-header>
<ion-item *ngFor="let note of (students | async)">
<ion-thumbnail slot="start">
<img src="assets/icon/check.png">
</ion-thumbnail>
<ion-label>
<h5>{{note.name}}</h5>
<p>Matrix:{{note.matrix}}</p>
<p>Booth:{{note.booth}}</p>
</ion-label>
<ion-button fill="outline" slot="end" [routerLink]="'/role/'+note.id">Evaluate</ion-button>
</ion-item>
</ion-list>
</ion-content>
Home.ts
import { Component, OnInit } from '@angular/core';
import { AuthenticateService } from '../services/authentication.service';
import { LoadingController } from '@ionic/angular';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { NavController, ModalController } from '@ionic/angular';
import { FirebaseService } from '../services/firebase.service'
import { AngularFirestore } from '@angular/fire/firestore';
import { FormControl } from '@angular/forms';
//import { debounceTime } from 'rxjs/operators';
import { studl } from '../modal/studl';
import { Observable } from 'rxjs';
@Component({
selector: 'app-studlist',
templateUrl: './studlist.page.html',
styleUrls: ['./studlist.page.scss'],
})
export class StudlistPage implements OnInit {
public students: Observable<studl[]>;
constructor(
public loadingCtrl: LoadingController,
private authService: AuthenticateService,
private fService: FirebaseService,
private firestore: AngularFirestore,
private router: Router,
private route: ActivatedRoute,
private navCtrl: NavController
) { }
ngOnInit() {
this.students = this.fService.getAllStud();
}
Studl.modal.ts
export interface studl {
id?: any;
name: string;
booth: string;
matrix: string;
cspmark:string;
exmark:string;
svmark:string;
}
有一个类似的问题,您需要的是 link 中的答案 above.If 答案没有为您服务,然后重新评论答案以解决它你.
您可以使用角度数组运算符进行搜索。只要你的对象是数组即可。
例如
this.students = this.students.filter((student) => student.booth.toLowerCase() === 'search Terms');
或者如果你想使用 observable,你可以使用 pipe 和 map。
ngOnInit() {
this.students = this.fService.getAllStud();
this.students.pipe
(
map(
students => {
return students.filter((student) =>
student.booth.toLowerCase() === 'search Terms');
}
)
)
}
我尝试了很多教程,但我找不到任何关于如何执行搜索的解决方案 function.i 想通过名为 booth 的字段在 StudentList 集合中搜索 如果有人愿意帮助我。所以这基本上是我的编码
firebase.service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument,AngularFirestoreCollection, DocumentReference } from '@angular/fire/firestore';
import { AngularFireAuth } from '@angular/fire/auth';
import { Subscription, Observable } from 'rxjs';
import {map, take} from 'rxjs/operators';
import {studl} from '../modal/studl';
@Injectable({
providedIn: 'root'
})
export class FirebaseService {
private students: Observable<studl[]>;
private studCollection: AngularFirestoreCollection<studl>;
constructor(
public afs: AngularFirestore,
public afAuth: AngularFireAuth) {
this.studCollection = this.afs.collection<studl>('StudentList');
this.students = this.studCollection.snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
});
})
);
}
//getall
getAllStud(): Observable<studl[]> {
return this.students;
}
//getsingle
getStudSingle(id: string): Observable<studl> {
return this.studCollection.doc<studl>(id).valueChanges().pipe(
take(1),
map(note => {
note.id = id;
return note;
})
);
}
}
}
Home.html
<ion-content>
<div class="title-block">
<div class="heading"></div>
</div>
<ion-searchba></ion-searchbar >
<ion-list>
<ion-list-header>
<ion-label>Select your student</ion-label>
</ion-list-header>
<ion-item *ngFor="let note of (students | async)">
<ion-thumbnail slot="start">
<img src="assets/icon/check.png">
</ion-thumbnail>
<ion-label>
<h5>{{note.name}}</h5>
<p>Matrix:{{note.matrix}}</p>
<p>Booth:{{note.booth}}</p>
</ion-label>
<ion-button fill="outline" slot="end" [routerLink]="'/role/'+note.id">Evaluate</ion-button>
</ion-item>
</ion-list>
</ion-content>
Home.ts
import { Component, OnInit } from '@angular/core';
import { AuthenticateService } from '../services/authentication.service';
import { LoadingController } from '@ionic/angular';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { NavController, ModalController } from '@ionic/angular';
import { FirebaseService } from '../services/firebase.service'
import { AngularFirestore } from '@angular/fire/firestore';
import { FormControl } from '@angular/forms';
//import { debounceTime } from 'rxjs/operators';
import { studl } from '../modal/studl';
import { Observable } from 'rxjs';
@Component({
selector: 'app-studlist',
templateUrl: './studlist.page.html',
styleUrls: ['./studlist.page.scss'],
})
export class StudlistPage implements OnInit {
public students: Observable<studl[]>;
constructor(
public loadingCtrl: LoadingController,
private authService: AuthenticateService,
private fService: FirebaseService,
private firestore: AngularFirestore,
private router: Router,
private route: ActivatedRoute,
private navCtrl: NavController
) { }
ngOnInit() {
this.students = this.fService.getAllStud();
}
Studl.modal.ts
export interface studl {
id?: any;
name: string;
booth: string;
matrix: string;
cspmark:string;
exmark:string;
svmark:string;
}
有一个类似的问题,您需要的是 link 中的答案 above.If 答案没有为您服务,然后重新评论答案以解决它你.
您可以使用角度数组运算符进行搜索。只要你的对象是数组即可。
例如
this.students = this.students.filter((student) => student.booth.toLowerCase() === 'search Terms');
或者如果你想使用 observable,你可以使用 pipe 和 map。
ngOnInit() {
this.students = this.fService.getAllStud();
this.students.pipe
(
map(
students => {
return students.filter((student) =>
student.booth.toLowerCase() === 'search Terms');
}
)
)
}