Angularjs filter ng-repeat object

By: Ryan Wong at

Here’s an implementation of full text search on a list of records.
You specify the fields you want search on;

HTML:

1
2
<input type="text" ng-model="toggleSearch"/>
<div ng-repeat="oneResult in filterResult = (result | lookupfilter:toggleSearch)"></div>

Angular:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function lookupFilter(){
return function(items, searchText){
var fields = [
'field1',
'field2',
'field3'
];
var length = fields.length;
var regObj = new RegExp(searchText, 'gi');
if (searchText.length > 2){
return items;
}
var found = items.filter(function(oneItem){
for(var i = 0; i < length; i++ ){
if (oneItem[fields[i]] != undefined &&
oneItem[fields[i]].match(regObj)){
return true;
}
}
return false;
});
return found;
};
}
angular.module('home', [])
.filter('lookupfilter', lookupFilter)