请使用模型名称ResProject和变量resProject:Observable;
这是我的代码:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Observable } from "rxjs";
@IonicPage()
@Component({
selector: 'page-project',
templateUrl: 'project.html',
})
export class ProjectPage {
resProject : Observable <ResProject[]> ;
constructor(public navCtrl: NavController,
public navParams: NavParams)
对于ResProject的模型,如下所示:
export interface ResProject {
key?: string;
type : string ;
name : string;
nbrUnit : number ;
nbrFloor : number ;
}
对于HTML,这是我的代码:
<ion-item *ngFor="let rp of resProject | async" detail-push navPush="ViewProjectPage" [navParams]="{resProject:rp} " >
{{rp.name}}
</ion-item>
请注意,我从Firebase获取Resproject的数据。
如何在
ion-serchbar
或任何其他解决方案中过滤此ResProject列表? 最佳答案
在您的情况下,您似乎正在使用Observables(Rxjs),而这些不是Arrays,因此您不能直接将Observables与.filter等数组方法一起使用。您可以使用Observable的运算符“ filter”来实现过滤器:https://www.learnrxjs.io/operators/filtering/filter.html
或者,您可以只在构造函数中订阅您的服务(返回Observable),然后使您的模板使用数组工作(因此,模板中不需要异步管道)。
尝试在TS中执行以下操作:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Observable } from "rxjs";
import { ResProject,ComProject,OffiProject } from "../../models/project.model";
import { AddProjectService } from "../../services/addproject.service";
@IonicPage()
@Component({
selector: 'page-project',
templateUrl: 'project.html',
})
export class ProjectPage {
// holders of original unfiltered data:
resProject : Observable <ResProject[]> ;
comProject : Observable <ComProject[]>;
// holders of data for filtered lists, initially set to empty array:
filteredResProject: any[] = [];
filteredComProject: any[] = [];
constructor(public navCtrl: NavController,
public navParams: NavParams,
public projectService : AddProjectService,
)
{
this.initializeItems();
}
filterItems(event) {
this.initializeItems();
const val = event.target.value;
if (val && val.trim() != '') {
this.filteredResProject = this.filteredResProject.filter((item) => {
return (item.name.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
initializeItems() {
this.resProject = this.projectService.getResProjectList()
.snapshotChanges()
.map(
changes => {
return changes.map(c => ({
key : c.payload.key, ...c.payload.val()
}))
}
)
this.resProject.subscribe( res => {
this.filteredResProject = res;
})
this.comProject = this.projectService.getComProjectList()
.snapshotChanges()
.map(
changes => {
return changes.map(c => ({
key : c.payload.key, ...c.payload.val()
}))
}
)
this.comProject.subscribe( res => {
this.filteredComProject = res;
});
}
}
而且在您的模板中,yoo应该不再使用异步管道,因为您仅渲染基于数组的数据:
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Project List</ion-title>
</ion-navbar>
<ion-toolbar>
<ion-searchbar (ionInput)="filterItems($event)"></ion-searchbar>
</ion-toolbar>
</ion-header>
<ion-content >
<ion-list>
<ion-list-header>
Project List
</ion-list-header>
<ion-badge>Number Of Residential Project :{{ resProject.length }}</ion-badge>
<ion-item *ngFor="let rp of filteredResProject" detail-push navPush="ViewProjectPage" [navParams]="{resProject:rp} ">
{{rp.name}}
</ion-item>
</ion-list>
<ion-list>
<ion-badge color="danger">Number Of Commercial Project :{{ comProject.length }}</ion-badge>
<ion-item *ngFor="let cp of filteredComProject" detail-push navPush="ViewComProPage" [navParams]="{comProject:cp}">
{{cp.name}}
</ion-item>
</ion-list>
</ion-content>
这不是最好的代码设计,但是根据您当前的代码,这应该可以工作。基本思想-您想从服务中获取完整的真实数据,并且在每次“初始化”时仅修改过滤后的列表。