如何在离子2(搜索栏)中自动完成 [英] How to do auto-complete in ionic 2 (search-bar)

查看:115
本文介绍了如何在离子2(搜索栏)中自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在搜索栏中自动完成我到目前为止所做的事情。

I am trying to do an auto complete in my search bar what i have done so far is.

我有一个包含一些字符串的数组。然后我试图在我的项目中列出我能够搜索特定项目。

I have an array with some strings. and then i am trying to list in my items it i am able to search the particualr item.

但我的要求是不要在列表中显示项目。我必须单击搜索栏,所有数组中的字符串应该来,我必须进行搜索。

But my requirement is not to display the items in a list. I have to make on clicking the search bar all the strings in array should come and the i have to make a search.

<ion-header>

  <ion-navbar>
    <ion-title>search</ion-title>
  </ion-navbar>
  <ion-toolbar primary >
    <ion-searchbar (ionInput)="getItems($event)" autocorrect="off"></ion-searchbar>
  </ion-toolbar>

</ion-header>


<ion-content padding>

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

</ion-content>

search.ts文件的代码:

Code for search.ts file:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

/*
  Generated class for the SearchPage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  templateUrl: 'build/pages/search/search.html',
})
export class SearchPage {
private searchQuery: string = '';
  private items: string[];

  constructor(private navCtrl: NavController) {
    this.initializeItems();
  }

  initializeItems() {
    this.items = [
      'Amsterdam',
      'Bogota',
    ]
  }

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

问题:


如何通过离子2中的自动完成获取数组的值。

How to get the value of an array through auto complete in ionic 2.


推荐答案

为了实现这一点,您只需要在代码中添加一些小东西。请查看此plunker

In order to achieve that, you just need to add a small thing to your code. Please take a look at this plunker.

就像你可以看到的那样,使用 showList 变量,我们只能在用户搜索过某些内容后显示结果。

Like you can see there, with the showList variable we can show the results only after the user has searched something.

  <ion-list *ngIf="showList">
    <ion-item *ngFor="let item of items">
      {{ item }}
    </ion-item>
  </ion-list>

我们首先将该变量设置为 false in 构造函数然后我们在 getItems(...)<中将它设置为 true / code>方法。

We first set that variable to false in the constructor and then we set it to true inside the getItems(...) method.

这篇关于如何在离子2(搜索栏)中自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆