I'm very new to Typescript and Ionic 2 and I'm trying to filter trough a json response with Ionic 2 search bar.
This is my code:
import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
posts: any;
private searchQuery: string = '';
private items: string[];
constructor(private http: Http) {
this.initializeItems();
this.http.get('https://domain.co/open.jsonp').map(res => res.json()).subscribe(data => {
this.posts = data;
console.log(this.posts);
});
}
initializeItems() {
this.items = this.posts;
}
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.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
}
And the Markup:
<ion-header>
<ion-searchbar (ionInput)="getItems($event)" [debounce]="500" placeholder="Suchen..."></ion-searchbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngFor="let post of posts">
<h1>{{post.storeName}}</h1>
</ion-item>
</ion-list>
</ion-content>
I this error when I search:
item.toLowerCase is not a function
The JSON data looks like this:
[
{
storeName: "Avec Hauptbahnhof",
addressLink: "",
phone: "0326223902",
image: "",
description: "",
link: "",
openingHours: [
"05.30 - 22:00",
"05.30 - 22:00",
"05.30 - 22:00",
"05.30 - 22:00",
"05.30 - 22:00",
"06.30 - 22:00",
"7.00 - 22.00"
]
},
{
storeName: "Manor",
addressLink: "",
phone: "0326258699",
image: "",
customer: "",
description: "",
link: "",
openingHours: [
"09.00 - 18.30",
"09.00 - 18.30",
"09.00 - 18.30",
"09.00 - 21:00",
"09.00 - 18.30",
"08.00 - 17.00",
"Geschlossen"
]
}
]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…