簡體   English   中英

Angular 搜索過濾器(由字符串數組組成)

[英]Angular search filter(Consists array of strings)

html:

<input matInput placeholder="Search" value="" formControlName="searchbar">

ts:

typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];

我需要顯示與數組數據匹配的數據。

string[] = ['靴子', '木屐', '樂福鞋', '莫卡辛鞋', '運動鞋'];

let searchKey;(你必須搜索的值)

讓 Search = string.find((a) =>a.includes(searchKey);

你在 serch 變量中得到匹配值

HTML

<mat-form-field>
   <input matInput [matAutocomplete]="autoComplete"
      placeholder="Search" [formControl]="searchControl">
   <mat-autocomplete #autoComplete>
      <mat-option *ngFor="let shoe of filteredShoes" [value]="shoe"> {{shoe}} </mat-option>
   </mat-autocomplete>
</mat-form-field>

TS

  searchControl = new FormControl();
  typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];
  filteredShoes: string[];

  ngOnInit() {
     this.searchControl.valueChanges.subscribe(searchSrting => {
        this.filteredShoes = this.getSearchResult(searchSrting).slice();
  });

  getSearchResult(searchString: string): string[] {
     return this.typesOfShoes.filter(x => x.toLowerCase().includes(searchString.toLowerCase()))
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM