【发布时间】:2017-07-20 10:30:00
【问题描述】:
我正在使用 PrimeNG Tree 组件使用户能够基于树结构选择一些值。选定的节点稍后将存储在数据库中,当用户再次访问编辑对话框时,应预先选择这些节点。
目前的 PrimeNG 版本有什么方法可以实现这一点吗?或者,如果您能告诉我另一个支持复选框选择和节点预选的 angular2 树组件,那就太好了。
【问题讨论】:
标签: angular typescript tree primeng
我正在使用 PrimeNG Tree 组件使用户能够基于树结构选择一些值。选定的节点稍后将存储在数据库中,当用户再次访问编辑对话框时,应预先选择这些节点。
目前的 PrimeNG 版本有什么方法可以实现这一点吗?或者,如果您能告诉我另一个支持复选框选择和节点预选的 angular2 树组件,那就太好了。
【问题讨论】:
标签: angular typescript tree primeng
在selectionMode="checkbox" 中选择的节点存储在[(selection)]="selectedNodesArray" 属性中。
您可以将数据库中的值放入selectedNodesArray,然后将选择此节点。
【讨论】:
这是以编程方式选择节点的方法:
HTML
<p-tree
[value]="list['Entities']"
[(selection)]="data['Entities']"
selectionMode="checkbox">
</p-tree>
方法
const selectNodes = (tree: TreeNode[], checkedNodes: TreeNode[], keys: string[]) => {
// Iterate through each node of the tree and select nodes
let count = tree.length;
for (const node of tree) {
// If the current nodes key is in the list of keys to select, or it's parent is selected then select this node as well
if (keys.includes(node.key) || checkedNodes.includes(node.parent)) {
checkedNodes.push(node);
count--;
}
// Look at the current node's children as well
if (node.children)
selectNodes(node.children, checkedNodes, keys);
}
// Once all nodes of a tree are looked at for selection, see if only some nodes are selected and make this node partially selected
if (tree.length > 0 && tree[0].parent) tree[0].parent.partialSelected = (count > 0 && count != tree.length);
}
调用方法
const keysToBeSelected = [2,3,4]
selectNodes(this.list.Entities, this.filterData.Entities, keysToBeSelected);
【讨论】:
找到了在 PrimeNG Tree 中预选多个复选框(以编程方式)的解决方法。你可以在这里找到工作示例:https://github.com/jigneshkhatri/primeng-treenode-preselect
【讨论】:
使用“复选框”设置 selectionMode 属性,如下所示:
<p-tree
selectionMode="checkbox"
[(selection)]="selectedNodes"
></p-tree>
selectedNodes 变量包含选定的节点。在此变量中添加您要选择的所有节点。
【讨论】: