Na maioria das vezes, precisamos filtrar os resultados nos atributos especificados. Podemos conseguir o mesmo seguindo o filtro personalizado no angular.js
testModule.service('ViewModel',function(){
return {
this.Results:[{Name:'',Age:'',DateOfBirth:'',Addess:{Line1:'',Line2:'',PostCode:''},Designation:''}],
this.FilterBy:'',
this.FilterCommand : function () {
if (this.FilterBy) {
this.FilteredResults = $filter('fieldFilter')(this.Results, this.FilterBy, ['Name', 'Address', 'DateOfBirth']);
} else {
this.FilteredResults = this.Results;
}
};
}
});
testModule.filter('fieldFilter',['$filter', function ($filter) {
return function (input, filterby, parameters) {
var result = [];
var search = function (obj, text) {
switch (typeof obj) {
case "boolean":
case "number":
case "string":
return obj.indexOf(text) > -1;
break;
case "object":
for (var objKey in obj) {
if (objKey.charAt(0) !== '$' && search(obj[objKey], text))
return true;
};
break;
default:
return false;
};
return false;
};
angular.forEach(input, function (item) {
angular.forEach(parameters, function (params) {
if (search(item[params], filterby)) {
result.push(item);
};
});
});
return result;
};
}]);