Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to filter Object Array Except one property

Angular $filter can do string fuzzy search for Object Array,

But every of My Objects have one property of base64 pic.

var MyObjects = [{
    property1: 'ab',
    property2: 'cd',
    pic: '4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBw.....' 
}, {
    property1: 'ef',
    property2: 'gh',
    pic: '4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBw.....' 
}, {
    ....


}],

result = $filter('filter')(MyObjects, $scope.searchText);

How can I except pic property in fuzzy search?

like image 545
Chen-Tsu Lin Avatar asked May 30 '14 15:05

Chen-Tsu Lin


1 Answers

Angular's filter can take a function as an argument to filter your array. The filter will select items that the function returns true for.

You can use this feature to achieve what you want.

Here is the official documentation

So, you could do something like this to compare the search text only with the two properties you want to:

var filterFunction = function(item) {
    var val = $scope.searchText
    return item.property1.indexOf(val || '') !== -1 || item.property2.indexOf(val || '') !== -1;
}

result = $filter('filter')(MyObjects, filterFunction, $scope.searchText);

Here's a fiddle demonstrating this effect.

like image 164
Nick Avatar answered Oct 12 '22 23:10

Nick