我在过滤嵌套对象时遇到了一些问题。在选择中我选择过滤器参数(对象值),在输入中输入一些在对象键中搜索它的文本。
尝试使用递归编写自定义过滤器进行深度搜索,但它不起作用。
input
是ng-repeat中对象的参数,param1
用于选择模型,param2
用于输入&ng-model模型。
的 JS
.filter('personFilter', function($filter) {
return function(input, param1, param2) {
var output = {};
for (var prop in input) {
if (typeof input[prop] == 'object' || prop != param1 && input[prop] != param2) {
$filter('personFilter')(input[prop]);
} else {
output[key] = input[key];
}
}
return output;
}
这就是吸食者:http://plnkr.co/edit/83lPNRWFy6wa9U2FkMfH?p=preview
我希望有人给我一些建议
答案 0 :(得分:0)
.filter('personFilter', function($filter) {
return function(input, recursive, search) {
return input.filter(filterFn);
function filterFn(obj){
var val, res;
for (var prop in obj) {
val = obj[prop];
if (typeof val == 'object' && recursive) {
recursive = false;
res = res || val.filter(filterFn).length;
recursive = true;
} else if(!recursive){
res = res || val == search;
}
}
console.log(res, obj, recursive, search);
return res;
}
}
以下标记
<body ng-controller="appCtrl">
<select ng-model="selectParameter" ng-options="item.value as item.key for item in parameter track by item.value">
</select>
<input ng-model="query" />
<div ng-repeat="person in object.node1.node2.persons | personFilter:selectParameter:query track by $index">
<!-- -->
<p>Person: {{person.name}}, Children: <span ng-repeat="child in person.children track by $index">{{child.name}}, </span></p>
</div>
</body>
和控制器初始化如:
$scope.parameter = [
{value: false, key: 'Person Name'},
{value: true, key: 'Child Name'}
];
$scope.selectParameter = true;