简体   繁体   English

使用 Angular 2 在 json 文件中搜索

[英]Searching in a json file with Angular 2

I know Angular 2 is old, but I used the best course our library had, and most of it still works...我知道 Angular 2 很旧,但我使用了我们图书馆最好的课程,而且大部分课程仍然有效......

I have a list of data in a json file, which looks something like this:我在 json 文件中有一个数据列表,它看起来像这样:

    {
        "name"          :"example one",
        "source"        :"",
        "source_type"   :"",
        "text"          :"A bumblebee in the field"
    },
    {
        "name"          :"example two",
        "source"        :"",
        "source_type"   :"",
        "text"          :"Two flowers with red petals"
    },

I can display the whole list of names and can access the other data too.我可以显示整个姓名列表,也可以访问其他数据。 Now I want to have a text field so the user can search.现在我想要一个文本字段,以便用户可以搜索。 I'd like a search option for name and one for text (even though the text doesn't display directly).我想要一个名称搜索选项和一个文本搜索选项(即使文本不直接显示)。 The problem is: I'd like users to be able to search for single words, like 'one' and get all results that contain the word 'one'.问题是:我希望用户能够搜索单个词,例如“one”,并获得包含“one”一词的所有结果。

Is this possible?这可能吗? Or would I be better of learning how to set up a database online and implement a search option from there?或者我会更好地学习如何在线设置数据库并从那里实施搜索选项?

This might give you the idea for the search using filter这可能会给您提供使用filter进行搜索的想法

 var searchText = "three"; var data = [ { name: "example one three", source: "", source_type: "", text: "A bumblebee in the field" }, { name: "example two", source: "", source_type: "", text: "Two flowers with red petals" }, { name: "example three", source: "", source_type: "", text: "Two flowers with red petals" } ]; let newObj = data.filter((value)=>{ return value.name.indexOf(searchText) != -1 ? value : null }); console.log(newObj);

Well, you could do this using a Pipe.好吧,你可以使用管道来做到这一点。 But using Filter Pipes is not really recommended by the Angular Team.但是 Angular 团队并不真正推荐使用过滤器管道。 So you can essentially listen to the keyup event on your input field and then call a function to filter the data.因此,您基本上可以监听输入字段上的keyup事件,然后调用函数来过滤数据。

Now, since this is a strict filter object by a key, you can simply use the filter method on Array and check if the indexOf the searchInput in the name or text is >-1现在,由于这是一个通过键的严格过滤器对象,您可以简单地在Array上使用filter方法并检查nametextsearchInputindexOf是否>-1

Here's how:就是这样:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  filterText;

  data = [{
    "name": "example one",
    "source": "",
    "source_type": "",
    "text": "A bumblebee in the field"
  },
  {
    "name": "example two",
    "source": "",
    "source_type": "",
    "text": "Two flowers with red petals"
  }];

  filteredData = [];

  ngOnInit() {
    this.filteredData = [...this.data];
  }

  onChange() {
    console.log('ran');
    this.filteredData = this.data.filter(
      datum => (datum.name.indexOf(this.filterText) > -1 || datum.text.indexOf(this.filterText) > -1));

    console.log(this.filteredData);

  }


}

And in the template:在模板中:

<input type="text" [(ngModel)]="filterText" (keyup)="onChange()">

<ul>
  <li *ngFor="let datum of filteredData">
    {{ datum.name }}
  </li>
</ul>

Here's a Sample StackBlitz for your ref.这是供您参考的示例 StackBlitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM