First, a little background. Basically, I am looking at a list of objects in a table using ng-repeat. My filter should be able to accept the search string and check each of the search terms in duplicate objects. Each of the search conditions must be present (in any order) in any of the properties of the object for the filter in order to pass this object. For example, if an object looks like this:
{
customer: 'John Smith',
detail: 'Some details'
}
And my search text smith johnor details john smithor smith john details, it should be able to accept these conditions in any order and find the corresponding objects. The above object must match because all search terms are present.
The filter I wrote below:
angular.module('app').filter('assetsearch', function () {
return function (input, searchString) {
var searchTerms = searchString === '' ? [] : searchString.replace(/ +/g, ' ').replace(/[^\w\s]|_/g, '').toLowerCase().split(' ');
var matches = _.filter(input, function (asset) {
var textToSearch = [
asset.customer,
asset.details
];
textToSearch = textToSearch.join(' ').replace(/ +/g, ' ').replace(/[^\w\s]|_/g, "").toLowerCase();
var match = true;
_.each(searchTerms, function (term) {
match = (textToSearch.trim().indexOf(term) > -1) && match;
});
return match;
});
return matches;
}
});
, $ctrl :
<tr ng-repeat="asset in $ctrl.assets | assetsearch:$ctrl.filters.search" ng-if="!asset.loading">
" ", "" "", - , ... " " " ", Joh Smith ', , , ( , , , ).