【发布时间】:2018-11-10 19:51:29
【问题描述】:
我有产品 api。在我的 search.html 页面上,我将显示从 json 文件中获取的所有产品的名称。如何根据 search.html 页面中的搜索栏获得名称。我使用了 Ionic 搜索栏组件,但它不起作用。请帮我。如果此功能有任何可用的替代方案,也请在此处发布。
search.ts
import { Component } from '@angular/core';
import { NavController} from 'ionic-angular';
import { ApiService } from '../../services/api.service';
@Component({
selector: 'page-search',
templateUrl: 'search.html'
// styleUrls: ['search.scss']
})
export class SearchPage {
searchQuery: string = '';
items: any[];
constructor(public navCtrl: NavController, private apiService: ApiService) {
this.initializeItems();
}
initializeItems() {
this.apiService.productsCall().subscribe(response => {
//console.log(response);
if(response['status'] == 200) {
// this.data=response['response'];
this.items = response['response'];
console.log(this.items);
} else if(response['status'] == 500) {
console.log(response['error'].sqlMessage);
}
});
}
getItems(ev: any) {
// Reset items back to all of the items
this.initializeItems();
// set val to the value of the searchbar
let val = ev.target.value;
// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
this.items = this.items.filter((item) => {
return (item['name'].toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
}
search.html
<ion-content padding>
<ion-searchbar (ionInput)="getItems($event)"></ion-searchbar>
<ion-list no-lines>
<ion-item *ngFor="let item of items">
<ion-label>{{item.name}}</ion-label>
<ion-avatar item-right>
<img src='../assets/imgs/medicalStore.png'>
</ion-avatar>
<ion-checkbox item-left></ion-checkbox>
</ion-item>
</ion-list>
</ion-content>
json 文件
{
"status":200,
"error":null,
"response":[
{
"product_id":1,"name":"knee cap","price":1290,"weight":0.4,"short_desc":"Neck Pain Relief Cervical Soft Pillo"
},
{
"product_id":2,"name":"soft pillow","price":1299,"weight":0.3,"short_desc":"Neck Pain Relief Cervical Soft Pillow "
}
]
}
【问题讨论】:
标签: angular typescript ionic-framework filter