首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angularjs总是在自定义过滤器中从初始数据过滤

Angularjs总是在自定义过滤器中从初始数据过滤
EN

Stack Overflow用户
提问于 2016-07-04 05:39:56
回答 0查看 47关注 0票数 0

我正在尝试使用一个自定义过滤器来过滤基于搜索词的数据。在以下contoller作用域中填充的作用域属性:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

自定义过滤器代码:

代码语言:javascript
复制
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,但这给了我错误。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38174575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档