所以,我正在嘗試創建一個分層樹。選擇具有子節點的節點,然后選擇節點的所有子項,但是當我選擇所有子項時,我也想要選擇父母。
這是 plunker 的鏈接:[https://plnkr.co/plunk/iMBFfy6cf7urOHhZ][1]
我創建了一個目錄來標記樹
樹控制器.js
(function (ng) {
var app = ng.module('tree', ['tree.service', 'tree.directives']);
app.controller("TreeController", ["TreeService", "$scope", function (TreeService, $scope) {
var tc = this;
buildTree();
function buildTree() {
TreeService.getTree().then(function (result) {
tc.tree = result.data;
}, function (result) {
alert("Tree no available, Error: " result);
});
}
$scope.selectedItems = [];
$scope.getSelected = function(){
$scope.selectedItems = [];
function checkChildren(c) {
angular.forEach(c.children, function (c) {
if (c.checked){
$scope.selectedItems.push({"selected":c.name});
}
checkChildren(c);
});
}
angular.forEach(tc.tree, function(value, key) {
if (value.checked){
$scope.selectedItems.push({"selected":value.name});
}
checkChildren(value);
});
};
}]);
})(angular);
索引.html
<div ng-controller="TreeController as tc">
<ul class="tree">
<node-tree children="tc.tree"></node-tree>
</ul>
<button ng-click="getSelected()">Get Selected</button>
<br/>
<br/>
Selected:
<ul>
<li ng-repeat="item in selectedItems">
{{item.selected}}
</li>
</ul>
</div>
樹指令.js
(function (ng) {
var app = ng.module('tree.directives', []);
app.directive('nodeTree', function () {
return {
template: '<node ng-repeat="node in tree"></node>',
replace: true,
restrict: 'E',
scope: {
tree: '=children'
}
};
});
app.directive('node', function ($compile) {
return {
restrict: 'E',
replace: true,
templateUrl: 'node.html', // HTML for a single node.
link: function (scope, element) {
/*
* Here we are checking that if current node has children then compiling/rendering children.
* */
if (scope.node && scope.node.children && scope.node.children.length > 0) {
scope.node.childrenVisibility = true;
var childNode = $compile('<ul ng-if="!node.childrenVisibility"><node-tree children="node.children"></node-tree></ul>')(scope);
element.append(childNode);
} else {
scope.node.childrenVisibility = false;
}
},
controller: ["$scope", function ($scope) {
// This function is for just toggle the visibility of children
$scope.toggleVisibility = function (node) {
if (node.children) {
node.childrenVisibility = !node.childrenVisibility;
}
};
// Here We are marking check/un-check all the nodes.
$scope.checkNode = function (node) {
node.checked = !node.checked;
// if (node.checked){
// alert("clicked");
// }
function checkChildren(c) {
angular.forEach(c.children, function (c) {
c.checked = node.checked;
checkChildren(c);
});
}
checkChildren(node);
};
}]
};
});
})(angular);
節點.html
<li>
<span ng-click="toggleVisibility(node)"> {{ ( node.childrenVisibility && node.children.length ) ? ' ' : '-' }}</span>
<input ng-click="checkNode(node)" type="checkbox" ng-checked="node.checked">
<span>
{{ $index 1 }}. {{ node.name }}
</span>
</li>
uj5u.com熱心網友回復:
第一步是確定每個節點的父節點是什么。我們可以通過在樹加載后立即遞回并parent在每個節點上設定一個屬性來做到這一點。
樹控制器.js
...
function buildTree() {
TreeService.getTree().then(function (result) {
tc.tree = result.data;
function setParentForChildren(n) {
angular.forEach(n.children, function (c) {
c.parent = n;
setParentForChildren(c);
})
}
angular.forEach(tc.tree, setParentForChildren);
}, function (result) {
alert("Tree no available, Error: " result);
});
}
...
現在,我們可以在每次選中一個框時使用該父參考來遞回樹,并為每個父節點說“如果我的所有孩子都被選中,那么我也應該被選中”。
樹指令.js
...
$scope.checkNode = function (node) {
node.checked = !node.checked;
function checkParent(n) {
if (!n.parent)
return;
const p = n.parent;
p.checked = p.children.every(function(c) { return c.checked });
checkParent(p);
}
checkParent(node);
function checkChildren(c) {
angular.forEach(c.children, function (c) {
c.checked = node.checked;
checkChildren(c);
});
}
checkChildren(node);
};
...
鏈接到修改后的plunker
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312558.html
