AngularJS 在深度为 n 的树中过滤
AngularJS filter in tree with n depth
我也想这样做 question
对于具有 n 深度的数组。我有相同的 json 和 id、name 和 parent_id(不是类型)。我尝试过使用 ng-template 但它对我不起作用。
我怎样才能做到这一点?
我知道您无法使用 question 中提供的树解决方案
所以这里是 fiddle 使用 angularjs
的用户树
<ul>
<!--only get top level nodes where parent_id=0 though filtering -->
<li ng-repeat="user in users | filter:{parent_id:0}:true" ng-include="'userTree'"></li>
</ul>
<!--inside template filter to only get children of this user-->
<script type="text/ng-template" id="userTree">
{{user.name}}
<ul ng-if="has_children(user)" style="margin-left:10px">
<li ng-repeat="child in users | filter:{parent_id:user.id}:true " ng-include="'userTree'" ng-init="user=child">
</li>
</ul>
</script>
在控制器中:
$scope.has_children = function(user) {
for (i = 0; i < $scope.users.length; i++) {
if (user.id == $scope.users[i].parent_id) {
return true
}
}
return false
}
这是工作 link fiddle:
我也想这样做 question 对于具有 n 深度的数组。我有相同的 json 和 id、name 和 parent_id(不是类型)。我尝试过使用 ng-template 但它对我不起作用。
我怎样才能做到这一点?
我知道您无法使用 question 中提供的树解决方案 所以这里是 fiddle 使用 angularjs
的用户树<ul>
<!--only get top level nodes where parent_id=0 though filtering -->
<li ng-repeat="user in users | filter:{parent_id:0}:true" ng-include="'userTree'"></li>
</ul>
<!--inside template filter to only get children of this user-->
<script type="text/ng-template" id="userTree">
{{user.name}}
<ul ng-if="has_children(user)" style="margin-left:10px">
<li ng-repeat="child in users | filter:{parent_id:user.id}:true " ng-include="'userTree'" ng-init="user=child">
</li>
</ul>
</script>
在控制器中:
$scope.has_children = function(user) {
for (i = 0; i < $scope.users.length; i++) {
if (user.id == $scope.users[i].parent_id) {
return true
}
}
return false
}
这是工作 link fiddle: