【问题标题】:How can I parse this complex JSON data and add a search function in ionic 3?如何解析这个复杂的 JSON 数据并在 ionic 3 中添加搜索功能?
【发布时间】:2019-01-26 21:20:05
【问题描述】:
  1. 我有数组中列出的数据列表
  2. 我希望将所有这些都列在我的 html 中(如果可能,使用标题/分隔符进行分段)
  3. 我希望搜索功能正常工作

我找不到更好的方法来问这个问题,我相信这是一个非常简单的问题,但不知何故我就是不记得该怎么做。一世 用一些复杂的数据构建一个应用程序,虽然这不是 实际上,我已经创建了类似的东西来复制挑战。

这是我的 data.JSON

{  
   "list1":[  
      {  
         "id":"0",
         "title":"List 1 of 1",
         "content":"I am the 1st of list1"
      },
      {  
         "id":"1",
         "title":"List 2 of 1",
         "content":"I am the 2nd of list1"
      },
      {  
         "id":"2",
         "title":"List 3 of 1",
         "content":"I am the 3rd of list1"
      }
   ],
   "list2":[  
      {  
         "id":"0",
         "title":"List 1 of 2",
         "content":"I am the 1st of list2"
      },
      {  
         "id":"1",
         "title":"List 2 of 2",
         "content":"I am the 2nd of list2"
      },
      {  
         "id":"2",
         "title":"List 3 of 2",
         "content":"I am the 3rd of list2"
      }
   ],
   "list3":[  
      {  
         "id":"0",
         "title":"List 1 of 3",
         "content":"I am the 1st of list3"
      },
      {  
         "id":"1",
         "title":"List 2 of 3",
         "content":"I am the 2nd of list3"
      },
      {  
         "id":"2",
         "title":"List 3 of 3",
         "content":"I am the 3rd of list3"
      }
   ]
}

在我的 provider.ts 中

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import "rxjs/add/operator/map";
import "rxjs/add/operator/do";

@Injectable()
export class TtProvider {

  private url: string = "assets/data/data.json";

  constructor(public http: HttpClient) {

    console.log('Hello TtProvider Provider');
  }


  getData(){
    return this.http.get(this.url)
    .map(res => res )

  }

}

interface Lists{
  id: number,
  title:string,
  content:string,
 }

在我的listing.ts中

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TtProvider } from "../../providers/tt/tt";
import { DetailPage } from '../detail/detail';

@IonicPage()
@Component({
  selector: 'page-listings',
  templateUrl: 'listings.html',
})
export class ListingsPage {

  lists:Lists[];
  listSearch: any[];

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public dataapi: TtProvider) {

      this.dataapi.getData().subscribe(data =>{
        console.log ("data has loaded here", data);
        this.lists = data[''];
        this.listSearch = data[''];
      });
  }


  initializeItems() {
    this.lists = [...this.listSearch];
  }

  getItems(ev) {
   this.initializeItems();
   var val = ev.target.value;
      if (val && val.trim() != '') {
        this.lists = this.lists.filter((list) => {
            return (list.title.toString().toLowerCase().indexOf(val.toLowerCase()) > -1);
        })
    }
  }


  viewlist(list){
    console.log('I have loaded item', list);
    this.navCtrl.push(DetailPage, {list:list})
  }

}

interface Lists{
  id: number,
  title:string,
  content:string,
 }

我的listing.html如下

<ion-header>

  <ion-navbar>
    <ion-title>listings</ion-title>
  </ion-navbar>

  <ion-searchbar 
  (ionInput)="getItems($event)">     
  </ion-searchbar>

</ion-header>


<ion-content padding>

  <ion-list >
    <ion-item *ngFor="let list of lists" >
      {{list.title}}
    </ion-item>
  </ion-list>

</ion-content>

运行之后,我有以下内容

【问题讨论】:

  • 但是我没有看到你的数据被提取的错误,即你的列表得到了很好的安慰,你能否详细说明你的问题,至于这些慢速网络检测到的错误,请访问stackoverflow.com/questions/40143098/…以了解为什么这样做他们出现
  • 我在控制台上得到它,但我如何让它们在我的 html 中列出?我试过“list.title”我什么也没得到。

标签: angular ionic2 ionic3


【解决方案1】:

在您的数据加载调用中,即...,constructor() 将对象推送或分配给列表

   this.dataapi.getData().subscribe(data =>{
        console.log ("data has loaded here", data);
        this.lists = data;
        this.listSearch = data;
      });

我认为不应该成为问题的一件事是您的数组声明尝试将其更改为

lists:Lists[];
  listSearch: any[];

那么您将在您的 html 中迭代一个数组

【讨论】:

  • 我想我已经声明了这个lists:Lists[]; listSearch: any[]; 或者我应该在哪里声明它?此外,如果我这样做:this.dataapi.getData().subscribe(data =&gt;{ console.log ("data has loaded here", data); this.lists = data; this.listSearch = data; }); 我收到此错误 ERROR 错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays。
  • ok 把这行this.lists=data改成this.list.push(data)@VinNwaikwu
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多