2017-02-11 68 views
0

我想在我的页面上有一个搜索过滤器。但是,它好像总是返回undefined。以下是我的手稿:Ionic 2:搜索过滤器返回undefined

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

}); 

} 

我console.log(this.loadcategory),但显然它返回未定义。我的代码中有没有出现错误的地方?任何帮助将非常感激。

+0

您应该创建一个管道导致这就是管道的制造。你可以在这里查看一个例子https://github.com/VadimDez/ng2-filter-pipe/blob/master/src/ng2-filter.pipe.ts – misha130

+0

如何在模板中使用searchTerm? –

回答

0

是的,你有一个小错误..你的this.category.filter回电有回报,但filterItems实际上并没有返回任何值。 假设this.category是一个数组,尝试:

 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 
    }