Last active
August 29, 2015 14:21
-
-
Save drasive/d0a6a5b957e5059ac187 to your computer and use it in GitHub Desktop.
AngularJS - OR filter for multiple, specific properties filtered by a single search string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.filter('orFilter', function () { | |
return function (objects, properties, comparator) { | |
if (!objects|| objects.length === 0) { | |
return []; | |
} | |
if (!properties || properties.length === 0) { | |
return []; | |
} | |
if (!comparator || comparator.trim() === '') { | |
return objects; | |
} | |
var filteredObjects = []; | |
objects.forEach(function(object) { | |
var objectMatches = false; | |
// Check if any object expression matches the comparator | |
properties.forEach(function (property) { | |
if (!objectMatches && object[property].toLowerCase().indexOf(comparator.toLowerCase()) > -1) { | |
objectMatches = true; | |
} | |
}); | |
// Add the current object to the list of filtered objects | |
if (objectMatches) { | |
filteredObjects.push(object); | |
} | |
}); | |
return filteredObjects; | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input ng-model="query"> | |
<tr ng-repeat="object in objects | orFilter:['property1', 'property2', 'property3']:query"> | |
... | |
</tr> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment