2017-10-04 26 views
0

나는 ng-repeat에있는 필터를 가지고 있으며 중첩 된 개체의 문자열을 검색 문자열과 비교합니다. 검색 문자열이 객체에서 발견되면 true를 반환합니다.모든 자식 요소를 반환하는 중첩 된 개체의 필터

검색 문자열이 개체의 문자열과 일치 할 때 필터는 해당 개체에 대해 true를 반환하고 일치하는 개체의 모든 중첩 개체에 대해 true를 반환하도록이 기능을 확장하는 방법을 찾고 있습니다. 이 트리보기, 노드를 검색하고 일치하는 모든 자식 노드를 표시하려면).

어떻게하면됩니까?

내 필터는 다음과 같다 : I 발견

.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; 
      } 
     }; 
    }; 
    }); 

답변

0

용액 필터의 isString 부분의 함수를 이용하고, 반복하여 수집된다. 객체를 찾으면 재귀 함수를 사용하여 자식임을 찾고 visibleAsAChild 속성을 설정합니다. 그런 다음 isObject 평가에 조건을 추가하여 visibleAsAChild 소품이 표시된 true을 반환합니다. 이것이 가장 효율적인 방법인지는 확실치 않지만 확실하게 작동합니다.

.filter('deepFilter', function ($filter) { 
var currentObject; 
var setChildrenToVisible = function(node) { 
    angular.forEach(node.nodes, function(node) { 
    if(node.nodes) { 
     setChildrenToVisible(node); 
    } 
    node.visibleAsAChild = true; 
    }); 
}; 
var lookupChildren = function(o, value) { 
    // console.log(o); 
    angular.forEach(o.nodes, function(node) { 
    if (node.name === value) { 
     setChildrenToVisible(node); 
    } 
    }); 
}; 

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); 
       }); 
       if(found && value.hasOwnProperty('id')) { 
       currentObject = value; 
       } 
       if(value.hasOwnProperty('id') && value.visibleAsAChild) { 
       return true; 
       } 
       return found; 

      } else if (angular.isString(value)) { 
       if (value.indexOf(searchTerm) !== -1) { 
       if(currentObject){ 
        lookupChildren(currentObject, value); 
       } 
        return true; 
       } else { 
        return false; 
       } 
      } 
     } else { 
      return true; 
     } 
    }; 
};