我正在尝试使用一个自定义过滤器来过滤基于搜索词的数据。在以下contoller作用域中填充的作用域属性:
naApp.controller('naCareNewTicketCtrl', ['$scope', '$http', '$window', 'careservice', 'ptypesFilter',function($scope, $http, $window, ptypesFilter) {
$scope.problemtypes = [{
"name": "Fruit",
"subtypes": [{
"name": "Seeded",
"subsubtypes": [{
"name": "Apple"
}, {
"name": "Oranges"
}]
}, {
"name": "Acidic",
"subsubtypes": [{
"name": "Lemon"
}, {
"name": "Orange"
}]
}]
}];
}]);HTML:
<div ng-repeat="problem in problemtypes | ptypes:search">
<ul>
<li ng-if="problem.subtypes && problem.subtypes.length>0">{{problem.name}}</li>
<li class="main-probs ellipsify no-child" ng-if="!(problem.subtypes && problem.subtypes.length>0)">{{problem.name}}</li>
<ul ng-repeat="subproblem in problem.subtypes">
<li ng-if="subproblem.subsubtypes && subproblem.subsubtypes.length>0">{{subproblem.name}}</li>
<li class="sub-probs ellipsify no-child" ng-if="!(subproblem.subsubtypes && subproblem.subsubtypes.length>0)">{{subproblem.name}}</li>
<li class="sub-sub-probs ellipsify no-child" ng-repeat="subsubproblem in subproblem.subsubtypes" ng-click="selectproblem(problem, subproblem, subsubproblem )">{{subsubproblem.name}}</li>
</ul>
</ul>
</div>自定义过滤器代码:
naApp.filter('ptypesFilter', function() {
return function(items, searchText) {
if(searchText === undefined || searchText.length === 0){
return items;
} else {
var filteredItems = [];
for(var t = 0; t < items.length; t++){
var type = items[t];
// if item.name sub string matches then show entire item
var pName = type.name;
if(pName.indexOf(searchText) === 0){
filteredItems.push(type);
} else {
// check for sub types
if(type.subtypes !== undefined) {
var subtypes = [];
for(var st = 0; st < type.subtypes.length; st++){
var stype = type.subtypes[st];
var sPName = stype.name;
if(sPName.indexOf(searchText) === 0) {
subtypes.push(stype);
} else {
if(stype.subsubtypes !== undefined){
var subsubtypes = [];
for(var sst = 0; sst < stype.subsubtypes.length; sst++){
var sstype = stype.subsubtypes[sst];
var ssPName = sstype.name;
if(ssPName.indexOf(searchText) === 0){
subsubtypes.push(sstype);
}
}
if(subsubtypes.length > 0){
stype.subsubtypes = subsubtypes;
}
}
}
}
if(subtypes.length > 0){
type.subtypes = subtypes;
}
}
if(type.subtypes !== undefined && type.subtypes.length > 0){
filteredItems.push(type);
}
}
};
}
return filteredItems;
};
});与过滤一样,当子类型/子类型的名称属性与在数组中键入的文本匹配时,我只放入匹配的子类型/子类型,并将其分配回父类型。(我需要显示匹配类型的层次结构)当我删除所有文本时,如何才能取回原始对象,以便有未过滤的初始设置进行填充。在过滤之前,我试着在对象上做$.extend,但这给了我错误。
https://stackoverflow.com/questions/38174575
复制相似问题