0
我有一個位於ng-repeat
上的篩選器,它將所有對象(包括嵌套對象)的字符串與搜索字符串進行比較。如果在對象中找到搜索字符串,則返回true。篩選嵌套對象以返回所有子元素
我正在尋找一種方法來擴展此功能,以便當搜索字符串與對象中的字符串匹配時,過濾器將爲該對象返回true,並將爲匹配對象中的所有嵌套對象返回true(這是一個樹形視圖,我正在搜索一個節點,並希望在匹配時顯示所有子節點)。
我該怎麼做?
我的過濾器看起來是這樣的:
.filter('deepFilter', function ($filter) {
return function(text) {
return function (value) {
if(text && text.length > 0) {
var searchTerm = text;
if (angular.isObject(value)) {
var found = false;
angular.forEach(value, function(v) {
found = found || $filter('deepFilter')(searchTerm)(v);
});
return found;
} else if (angular.isString(value)) {
if (value.indexOf(searchTerm) !== -1) {
return true;
} else {
return false;
}
}
} else {
return true;
}
};
};
});