Angular 2 filter/search list

前端 未结 12 1364
眼角桃花
眼角桃花 2020-11-27 14:25

I\'m looking for the angular 2 way to do this.

I simply have a list of items, and I want to make an input whos job is to filter the list.



        
12条回答
  •  独厮守ぢ
    2020-11-27 14:41

    
    
    
    {{item.name}}
    filterResults() { if (!this.name) { this.filteredValue = [...this.items]; } else { this.filteredValue = []; this.filteredValue = this.items.filter((item) => { return item.name.toUpperCase().indexOf(this.name.toUpperCase()) > -1; }); } }

    Don't do any modification on 'items' array(list of items from which results are filtered). When searched item 'name' is empty return the complete list of 'items', if not compare the 'name' with every 'name' in the 'items ' array and filter out only the name that is present in 'items' array and store it in the 'filteredValue'.

提交回复
热议问题