2017-02-11 7 views
0

私のページに検索フィルターを設定したいと思います。しかし、それは常にundefinedを返すように見えます。イオン2:検索フィルターは未定義です

     filterItems(searchTerm) { 
    this.category.forEach((item) => { 
     return item.data.filter((data) =>{ 
      data.title.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1; 
     }) 
     }); 
} 

    setFilteredItems() { 
this.searchControl.valueChanges.debounceTime(500).subscribe(search => { 
    this.searching = false; 
    this.loadcategory = this.filterItems(this.searchTerm); 

    if (this.searchTerm == "") { 
    this.loadcategory = []; 
    this.searchresultcount = "0"; 
    } else { 
    this.loadcategory; 
    console.log(this.loadcategory) 
    } 

}); 

} 

は私が(this.loadcategory)CONSOLE.LOGが、どうやらそれはのようにundefinedを返します。以下は、私の活字体です。私のコードのどこかにどこかでエラーがありますか?どんな助けでも大歓迎です。

+0

をあなたは、これはパイプがために作られているものであるパイプの原因を作成する必要があります。this.categoryを想定し はしてみてください、配列です。ここで例を見ることができますhttps://github.com/VadimDez/ng2-filter-pipe/blob/master/src/ng2-filter.pipe.ts – misha130

+0

テンプレートで 'searchTerm'をどのように使いますか? –

答えて

0

はい、小さなエラーがあります。this.category.filterコールバックには戻り値がありますが、filterItemsは実際に値を返しません。

 filterItems(searchTerm) { 
     let temp:any[] =[]; 
     this.category.forEach((item) => { 
     temp.push(...item.data.filter((data) =>{ 
data.title.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1; 
     }) 
     ); 

     }); 
    return temp;//return from function 
    } 
関連する問題