我正在使用此http://plnkr.co/edit/U1TOccm3pURetRTUul12?p=preview来搜索json格式的多个对象关键字。来自plunker的代码:
angular
.module('demo', [])
.controller('ctrl', function ($scope) {
$scope.projects = [
{ id: 1, name: 'foo', keywords: 'aa bb cc' },
{ id: 2, name: 'bar', keywords: 'dd ee ff' },
{ id: 3, name: 'qux', lvl1: { lvl2: 'gg hh ii' } },
{ id: 4, name: 'baz', lvl1: { lvl2: { lvl3: 'gg hh ii' } } }
];
$scope.matchProject = function (project) {
// Match all projects if no terms are entered
if ($scope.searchText == null) return true;
// Match project if all terms are matched
var searchTerms = splitTerms($scope.searchText);
var projectTerms = walkTerms(project);
var unmatchedTerms = searchTerms.filter(function (searchTerm) {
return projectTerms.filter(function (projectTerm) {
return projectTerm.indexOf(searchTerm) !== -1;
}).length === 0;
});
return unmatchedTerms.length === 0;
};
function walkTerms(obj, terms) {
if (terms == null) {
terms = [];
}
for (var key in obj) {
// Ignore properties added by Angular
if (key.startsWith('$$')) continue;
// Ignore `null`, `undefined` or Array values
if (obj[key] == null || obj[key].constructor === Array) continue;
if (obj[key].constructor === Object) {
walkTerms(obj[key], terms);
continue;
}
if (typeof(obj[key]) === 'string') {
Array.prototype.push.apply(terms, splitTerms(obj[key]));
continue;
}
// Must be a boolean or number
terms.push(obj[key].toString());
}
return terms;
}
function splitTerms(text) {
if (text == null) return [];
return text
.toLowerCase()
.split(/\s+/)
.filter(function (term) {
return term.length > 0;
});
}
});
对我来说, $scope.projects
是一个项目对象列表。
$scope.projects = [
{ id: 1, name: 'foo', keywords: 'aa bb cc', image: //BLOB DATA HERE },
{ id: 2, name: 'bar', keywords: 'dd ee ff', image: //BLOB DATA HERE },
{ id: 3, name: 'qux', lvl1: { lvl2: 'gg hh ii' } },
{ id: 4, name: 'baz', lvl1: { lvl2: { lvl3: 'gg hh ii' } } }
];
我使用SQL查询获取对象并使用json_encode。我的一个对象键是MEDIUMBLOB
。 ng-repeat过滤器因此属性(dupes和notArray错误)而出现故障。
有没有办法解决这个问题,例如:排除BLOB属性被搜索?如何在plunker中排除该特定键?