简体   繁体   English

通过md-autocomplete Angular 4过滤列表

[英]Filter list by md-autocomplete Angular 4

I want to implement a feature when selecting timezone. 我要在选择时区时实施一项功能。 The list will filter its result as I type any value in the input filed. 当我在输入字段中键入任何值时,该列表将过滤其结果。 So the list will narrow down to a sublist only contains the what I typed there. 因此,该列表将缩小为仅包含我在此处键入的内容的子列表。

For example: 例如:

lsit = ["apple", "orange", "applepie", "appletart","pie","orangetart"];

so when I type "apple", then the list will narrow down to 因此,当我键入“ apple”时,列表将缩小为

["apple", "applepie", "appletart"];

The following code is what I did on my html file 以下代码是我对html文件所做的操作

<md-input-container class="full-width">
   <input name="timezone" type="text" [(ngModel)]="timezone" 
     (ngModelChange)="filterList($event)" placeholder="Select your timezone"
      mdInput [mdAutocomplete]="auto" class="full-width" required>

   <md-autocomplete #auto="mdAutocomplete" (optionSelected)='getDetails($event.option.value)'>
       <md-option *ngFor="let t of timezoneList" [value]="t">
           {{ t }}
       </md-option>
   </md-autocomplete>
</md-input-container>

ts file ts文件

timezoneList;
timezone;

ngOnInit() {
 this.timezoneList = momentTZ.tz.names();
}

getDetails(e) {
  console.log(e) . // this will capture the option result 
}

filterList(e) {
  console.log(e) // this will capture the input result
                 // type a, then e = a;
                 // type ap, then e = ap;
                 // type apple, then e = apple;
}

Because the timezoneList contains result like: 因为timezoneList包含如下结果:

 ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers",  
  "Africa/Asmara","Africa/Asmera", "Africa/Bamako", ...........]

So how should I filter this list while I am typing. 因此,我在键入时应该如何过滤此列表。 I tried to follow the Angular docs, but since my angular version is 4, it is not working properly. 我尝试按照Angular文档进行操作,但是由于我的Angular版本为4,因此无法正常工作。

Appreciate any help. 感谢任何帮助。

ngOnInit() {
 this.timezoneList = momentTZ.tz.names();
 this.originalTimezoneList = momentTZ.tz.names();
}

filterList(e) {

  this.timezoneList = originalTImezonelist.filter(x => x.includes(e)
}

You can do it by using Angular Customized Pipe. 您可以使用“角度自定义管道”来实现。 Please check this https://codeburst.io/create-a-search-pipe-to-dynamically-filter-results-with-angular-4-21fd3a5bec5c for full details. 请查看此https://codeburst.io/create-a-search-pipe-to-dynamically-filter-results-with-angular-4-21fd3a5bec5c了解详细信息。

in your app.component.ts: 在您的app.component.ts中:

<input ng-model="searchText" placeholder="enter search term here">
  <ul>
    <li ng-repeat="c in list | filter : searchText">
      {{ c }}
    </li>
  </ul>

in your filter.pipe.ts 在你的filter.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'filter'
})
export class FilterPipe implements PipeTransform {
  transform(items: any[], searchText: string): any[] {
    if(!items) return [];
    if(!searchText) return items;
searchText = searchText.toLowerCase();
return items.filter( it => {
      return it.toLowerCase().includes(searchText);
    });
   }
}

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

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