获取最终 child 或 grandchild 的 parent 元素?
get the parent element of the final child or grandchild?
我正在使用 angularjs,我从我的服务得到了这个响应,它以树的形式出现,我将该数据表示给 angular 树。这个object有4个主要的children,name和id。但是这些children和他们的children也有children,name和id。我想要的是无论我点击哪个 child ,我总是想访问主根 parent name thats "node1" "node2" "node3" 的值作为 name 所以我知道我的主要 parent 和我点击的 parent 是什么。
$scope.loadOrganizationTree = function () {
userManagementFactory.getOrganizationTree()
.success(function(data, status) {
if (JSON.stringify(data.statusType).indexOf("success") > -1) {
$scope.dataForTheTree = data.statusMsg;
}
} else {
$scope.setResponseMsgs(data, status);
}
}).error(function(data, status) {
$scope.userErrorMsg = data.statusMsg;
$scope.showErrorMSg = true ;
});
<div treecontrol class="tree-light accordion-org-tree"
style="height: 609px !important;" tree-model="dataForTheTree"
order-by="name" reverse-order="false" options="treeOptions"
on-selection="showSelected(node, selected, $parentNode, $index, $first, $middle, $last, $odd, $even)"
selected-node="node1" filter-expression="userProfileOrgSearch">
{{node.name}}
</div>
[
{
"id": 1,
"name": "node1",
"children": [
{
"id": 11,
"name": "node1.1",
"children": [
{
"id": 111,
"name": "node1.1.1",
"children": []
}
]
},
{
"id": 12,
"name": "node1.2",
"children": []
}
]
},
{
"id": 2,
"name": "node2",
"children": [
{
"id": 21,
"name": "node2.1",
"nodes": []
},
{
"id": 22,
"name": "node2.2",
"nodes": []
}
]
},
{
"id": 3,
"name": "node3",
"children": [
{
"id": 31,
"name": "node3.1",
"children": []
}
]
}
]
到目前为止,我已经尝试使用 $parentnode 进行访问,但它对我不起作用。请让我知道解决方案。
提前致谢。
加载数据后,您填写 $scope.dataForTheTree。然后你可以遍历树并设置每个节点的根。
我使用带有您的示例数据的 var dataForTheTree 为您编码。
在下面的 运行 之后,每个节点都有一个根 属性 设置为它的根节点。
var setRoot = function(elementWithChildren, root) {
elementWithChildren.root = root;
if (elementWithChildren.children != null) {
angular.forEach(elementWithChildren.children, function(child) { setRoot(child, root); });
}
};
angular.forEach(dataForTheTree, function(rootNode) { setRoot(rootNode, rootNode); });
我们获取树的每个节点并调用 setRoot() 函数。然后为每个 children 调用 setRoot 函数。
递归。
我正在使用 angularjs,我从我的服务得到了这个响应,它以树的形式出现,我将该数据表示给 angular 树。这个object有4个主要的children,name和id。但是这些children和他们的children也有children,name和id。我想要的是无论我点击哪个 child ,我总是想访问主根 parent name thats "node1" "node2" "node3" 的值作为 name 所以我知道我的主要 parent 和我点击的 parent 是什么。
$scope.loadOrganizationTree = function () {
userManagementFactory.getOrganizationTree()
.success(function(data, status) {
if (JSON.stringify(data.statusType).indexOf("success") > -1) {
$scope.dataForTheTree = data.statusMsg;
}
} else {
$scope.setResponseMsgs(data, status);
}
}).error(function(data, status) {
$scope.userErrorMsg = data.statusMsg;
$scope.showErrorMSg = true ;
});
<div treecontrol class="tree-light accordion-org-tree"
style="height: 609px !important;" tree-model="dataForTheTree"
order-by="name" reverse-order="false" options="treeOptions"
on-selection="showSelected(node, selected, $parentNode, $index, $first, $middle, $last, $odd, $even)"
selected-node="node1" filter-expression="userProfileOrgSearch">
{{node.name}}
</div>
[
{
"id": 1,
"name": "node1",
"children": [
{
"id": 11,
"name": "node1.1",
"children": [
{
"id": 111,
"name": "node1.1.1",
"children": []
}
]
},
{
"id": 12,
"name": "node1.2",
"children": []
}
]
},
{
"id": 2,
"name": "node2",
"children": [
{
"id": 21,
"name": "node2.1",
"nodes": []
},
{
"id": 22,
"name": "node2.2",
"nodes": []
}
]
},
{
"id": 3,
"name": "node3",
"children": [
{
"id": 31,
"name": "node3.1",
"children": []
}
]
}
]
到目前为止,我已经尝试使用 $parentnode 进行访问,但它对我不起作用。请让我知道解决方案。 提前致谢。
加载数据后,您填写 $scope.dataForTheTree。然后你可以遍历树并设置每个节点的根。 我使用带有您的示例数据的 var dataForTheTree 为您编码。 在下面的 运行 之后,每个节点都有一个根 属性 设置为它的根节点。
var setRoot = function(elementWithChildren, root) {
elementWithChildren.root = root;
if (elementWithChildren.children != null) {
angular.forEach(elementWithChildren.children, function(child) { setRoot(child, root); });
}
};
angular.forEach(dataForTheTree, function(rootNode) { setRoot(rootNode, rootNode); });
我们获取树的每个节点并调用 setRoot() 函数。然后为每个 children 调用 setRoot 函数。 递归。