Skip to content

Instantly share code, notes, and snippets.

@drasive
Last active August 29, 2015 14:21
Show Gist options
  • Save drasive/d0a6a5b957e5059ac187 to your computer and use it in GitHub Desktop.
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.
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;
};
});
<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