//an OR filter for name and number
angular.module('myApp', [])
.controller('Ctrl', function ($scope) {
$scope.users = [
{
firstName: 'John',
lastName: 'Stockton',
number: '12'
},
{
firstName: 'Michael',
lastName: 'Jordan',
number: '23'
},
{
firstName: 'Allen',
lastName: 'Iverson',
number: '3'
}
];
})
.filter('userSearch', function () {
return function (users, search) {
var matches = [];
angular.forEach(users, function (user) {
if (!angular.isDefined(users) ||
!angular.isDefined(search)) {
return false;
}
// initialize match conditions
var nameMatch = false,
numberMatch = false;
if (angular.isDefined(search.name) &&
search.name.length > 0) {
// substring of first or last name will match
if (angular.isDefined(user.firstName)) {
nameMatch = nameMatch ||
user.firstName.indexOf(search.name) > -1;
}
if (angular.isDefined(user.lastName)) {
nameMatch = nameMatch ||
user.lastName.indexOf(search.name) > -1;
}
}
if (angular.isDefined(user.number) &&
angular.isDefined(search.number)) {
// only match if number is exact match
numberMatch = user.number === search.number;
}
// either match should populate the results with user
if (nameMatch || numberMatch) {
matches.push(user);
}
});
// this is the array that will be fed to the repeater
return matches;
};
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.