htmlangularsearch-box

searchbox in angular 2 doesn't return the whole object


I'm very new to Angular2 and 4. I have a list of items of type class (Item). Items fields are name, price and description. I want to make a searchbox when the user types the name of the item, it displays the correct item object.

I followed this example: http://www.angulartutorial.net/2017/03/simple-search-using-pipe-in-angular-2.html but It didnt work, I think because if was searching between strings not objects of type item.


Solution

  • If you used the code in the tutorial, with an array of objects you just need to update the return statement of the transform method in your Angular2 Pipe like this:

    return value.filter(function (el: any) {
                    return el.name.toLowerCase().indexOf(input) > -1;
                })
    

    PS: I added el.name but you can search through the description or whatever you like.