angular treeview-获取父节点
angular treeview- get parent node
我正在使用 angular treeview 使用 angularjs 构建一棵树。一旦我 select 一个节点,我想知道 selected 节点的 'parent node'。
我不想在 json 数据中添加名为 'parent' 的属性,也不想创建任何父子键值对数组。
可以在初始文件中进行哪些更改以获得结果?
HTML
<div ng-app="myApp">
<div ng-controller="myController">
<div style="display:table-row; width:500px;">
<div style="display:table-cell; width:250px;">
<div style="margin:10px 5px 30px 5px; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>First Tree's Selected Node</b> : <br>{{tree01.currentNode.roleName}}</span>
</div>
</div>
<div style="display:table-cell; width:250px;">
<div style="margin:10px 5px 30px 5px; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>Second Tree's Selected Node</b> : <br>{{tree02.currentNode.roleName}}</span>
</div>
</div>
</div>
<div style="display:table-row; width:500px;">
<div style="display:table-cell; width:250px;">
<!-- FIRST TREE -->
<div data-angular-treeview="true" data-tree-id="tree01" data-tree-model="roleList1" data-node-id="roleId" data-node-label="roleName" data-node-children="children">
</div>
</div>
<div style="display:table-cell; width:250px;">
<!-- SECOND TREE -->
<div data-angular-treeview="true" data-tree-id="tree02" data-tree-model="roleList2" data-node-id="roleId" data-node-label="roleName" data-node-children="children">
</div>
</div>
</div>
</div>
</div>
试试这些改变。
在标记中:(添加 ng-click 属性)
<div
data-angular-treeview="true"
data-tree-id="tree01"
data-tree-model="roleList1"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
ng-click="printParent($event);">
</div>
在控制器中
$scope.printParent = function ($event) {
var root = $scope;
var currentScope = angular.element($event.target).scope();
console.log('selected Node details: ', currentScope.node);
currentScope = currentScope.$parent;
console.log('parents::')
while(currentScope.$id !== root.$id) {
console.log(currentScope.node);
currentScope = currentScope.$parent;
}
}
在指令中访问父级:
单击处理程序
scope[treeId].selectNodeLabel =
function( selectedNode, selectedNodeScope ){
if (selectedNodeScope) {
var parentScope = selectedNodeScope.$parent,
index,
targetArray;
if (parentScope.node) {
targetArray = parentScope.node.children;
} else {
//root node
targetArray = scope[treeModel];
}
// to insert after the index;
if (targetArray.length) {
index = targetArray.indexOf(selectedNode);
console.log('index :: ', index, 'parent::', targetArray);
if (index !== -1) {
//targetArray.splice(index, 0, newNodeToBeAdded)
}
}
}
}
// other stuff
}
在指令模板中
<span data-ng-class="node.selected" data-ng-click="' + treeId + '.selectNodeLabel(node, this)">{{node.' + nodeLabel + '}}</span>
您可以像 this.In MarkUp
一样简单地打印父级
<div
data-angular-treeview="true"
data-tree-id="tree01"
data-tree-model="roleList1"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
ng-click="printParent(this)">
</div>
在控制器中
$scope.printParent = function(item) {
var parent = item.$modelValue;
Console.log(parent);
};
我正在使用 angular treeview 使用 angularjs 构建一棵树。一旦我 select 一个节点,我想知道 selected 节点的 'parent node'。
我不想在 json 数据中添加名为 'parent' 的属性,也不想创建任何父子键值对数组。
可以在初始文件中进行哪些更改以获得结果?
HTML
<div ng-app="myApp">
<div ng-controller="myController">
<div style="display:table-row; width:500px;">
<div style="display:table-cell; width:250px;">
<div style="margin:10px 5px 30px 5px; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>First Tree's Selected Node</b> : <br>{{tree01.currentNode.roleName}}</span>
</div>
</div>
<div style="display:table-cell; width:250px;">
<div style="margin:10px 5px 30px 5px; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>Second Tree's Selected Node</b> : <br>{{tree02.currentNode.roleName}}</span>
</div>
</div>
</div>
<div style="display:table-row; width:500px;">
<div style="display:table-cell; width:250px;">
<!-- FIRST TREE -->
<div data-angular-treeview="true" data-tree-id="tree01" data-tree-model="roleList1" data-node-id="roleId" data-node-label="roleName" data-node-children="children">
</div>
</div>
<div style="display:table-cell; width:250px;">
<!-- SECOND TREE -->
<div data-angular-treeview="true" data-tree-id="tree02" data-tree-model="roleList2" data-node-id="roleId" data-node-label="roleName" data-node-children="children">
</div>
</div>
</div>
</div>
</div>
试试这些改变。
在标记中:(添加 ng-click 属性)
<div
data-angular-treeview="true"
data-tree-id="tree01"
data-tree-model="roleList1"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
ng-click="printParent($event);">
</div>
在控制器中
$scope.printParent = function ($event) {
var root = $scope;
var currentScope = angular.element($event.target).scope();
console.log('selected Node details: ', currentScope.node);
currentScope = currentScope.$parent;
console.log('parents::')
while(currentScope.$id !== root.$id) {
console.log(currentScope.node);
currentScope = currentScope.$parent;
}
}
在指令中访问父级:
单击处理程序
scope[treeId].selectNodeLabel =
function( selectedNode, selectedNodeScope ){
if (selectedNodeScope) {
var parentScope = selectedNodeScope.$parent,
index,
targetArray;
if (parentScope.node) {
targetArray = parentScope.node.children;
} else {
//root node
targetArray = scope[treeModel];
}
// to insert after the index;
if (targetArray.length) {
index = targetArray.indexOf(selectedNode);
console.log('index :: ', index, 'parent::', targetArray);
if (index !== -1) {
//targetArray.splice(index, 0, newNodeToBeAdded)
}
}
}
}
// other stuff
}
在指令模板中
<span data-ng-class="node.selected" data-ng-click="' + treeId + '.selectNodeLabel(node, this)">{{node.' + nodeLabel + '}}</span>
您可以像 this.In MarkUp
一样简单地打印父级 <div
data-angular-treeview="true"
data-tree-id="tree01"
data-tree-model="roleList1"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
ng-click="printParent(this)">
</div>
在控制器中
$scope.printParent = function(item) {
var parent = item.$modelValue;
Console.log(parent);
};