angular - 带有过滤器的搜索栏和带有 Ionic 2 的 JSON 数据

标签 angular typescript ionic-framework ionic2 ionic3

我是 Typescript 和 Ionic 2 的新手,我正在尝试使用 Ionic 2 搜索栏过滤 json 响应。

这是我的代码:

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);
      })
    }
  }

}

和标记:

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

我在搜索时出现这个错误:

item.toLowerCase is not a function

JSON 数据如下所示:

[
{
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"
]
}
]

最佳答案

你得到那个错误是因为每个 item 不是一个字符串,而是一个对象,所以不要做

item.toLowerCase().indexOf(val.toLowerCase()) > -1

你应该这样做

item.storeName.toLowerCase().indexOf(val.toLowerCase()) > -1

另请注意,在您看来,您正在使用posts 数组

*ngFor="let post of posts" 

但是您应该改用items 数组,因为那是要被过滤的数组

  <ion-list>
    <ion-item *ngFor="let item of items">
      <h1>{{item.storeName}}</h1>
    </ion-item>
  </ion-list>

除此之外,我会做一些不同的事情,只是为了确保用户能够在数据可用时使用该页面(因为您使用的是 http请求获取它)。为此,我将添加一个加载警报,并在 http 请求完成后立即将其删除。从 Ionic2-beta.11 开始,您可以这样做:

import { Component } from '@angular/core';
import { NavController, LoadingController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {

  private posts: any; // <- I've added the private keyword 
  private searchQuery: string = '';
  private items: any; // <- items property is now of the same type as posts
  constructor(private http: Http, private loadingCtrl: LoadingController) {

    // this.initializeItems(); <- you don't need this anymore

    // Show the loading message
    let loadingPopup = this.loadingCtrl.create({
      content: 'Loading posts...'
    });

    this.http.get('https://domain.co/open.jsonp').map(res => res.json()).subscribe(data => {
        this.posts = data;
        this.initializeItems();

        // Hide the loading message
        loadingPopup.dismiss();
    });
  }

  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.storeName.toLowerCase().indexOf(val.toLowerCase()) > -1);
      })
    }
  }

}

关于angular - 带有过滤器的搜索栏和带有 Ionic 2 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39055621/

相关文章:

angular - 在强制转换的 Typescript 类上调用方法

javascript - firebase 部署显示空白索引文件,不显示任何应用程序

css - 更改标签栏高度

css - 自定义 ionic 复选框默认背景色

reactjs - Jestjs - FluentUI - 抑制 "The icon ' X' 被使用但未注册”警告

javascript - 存储从外部摄像头拍摄的图像并将其存储在数据库中 - Ionic 3

html - 将删除线元素保存到待办事项列表的本地存储

angular - Angular Material mat-select中的值必须是多选模式下的数组

angular - 除了 app.component 的 ngOninit 之外,在哪里放置 api 调用以使系统等待响应返回并注入(inject)到主体中?

javascript - 处理对本地状态的并发异步更新