gpt4 book ai didi

javascript - 如何选择素 Angular p树上的所有节点?

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:44 24 4
gpt4 key购买 nike

我在 p-tree (primeng) 上有这些数据,我想知道是否可以将所有选定的值放入初始加载中。我试图在“selectedFiles”上放置一个新数组,其中包含当前数据,父级和子级处于同一级别,在数组上,仅适用于父节点,但不适用于子节点。

数据:

 this.filesTree = [
{
"label": "Documents",
"data": "Documents Folder",
"expandedIcon": "fa-folder-open",
"collapsedIcon": "fa-folder",
"children": [{
"label": "Work",
"data": "Work Folder",
"expandedIcon": "fa-folder-open",
"collapsedIcon": "fa-folder",
"children": [{"label": "Expenses.doc", "icon": "fa-file-word-o", "data": "Expenses Document"}, {"label": "Resume.doc", "icon": "fa-file-word-o", "data": "Resume Document"}]
},
{
"label": "Home",
"data": "Home Folder",
"expandedIcon": "fa-folder-open",
"collapsedIcon": "fa-folder",
"children": [{"label": "Invoices.txt", "icon": "fa-file-word-o", "data": "Invoices for this month"}]
}]
}
];

Angular 代码:

export class TreeDemo implements OnInit {

msgs: Message[];

@ViewChild('expandingTree')
expandingTree: Tree;
selectedFile: TreeNode;
constructor(private nodeService: NodeService) { }

ngOnInit() {

this.nodeService.getFiles().then(files => this.filesTree = files);
}

selectAll(){
// with the parent nodes is working
this.selectedFiles = this.filesTree.map(
files => {
... files
})
//this is an example of how I want to store but is not working
this.filesTree
.map(files => {
this.selectedFiles = files.children
.map( file => {
return {
... file,
parent: files
};
});
});

}

模板:

<h3>Multiple Selection with Checkbox</h3>
<p-tree
[value]="filesTree"
selectionMode="checkbox"
[(selection)]="selectedFiles">
</p-tree>
<div>Selected Nodes:
<span *ngFor="let file of selectedFiles2">{file.label} </span>
</div

最佳答案

一、将所有节点添加到数组的方法

flattenTree(node : TreeNode)
{
this.arr.push(node);

if (node.children)
{
node.children.forEach(childNode => {
this.flattenTree(childNode);
} );
}
}

然后设置 selectedItems = 所有节点的数组

test(node)
{
this.filesTree11.forEach(node => {
this.flattenTree(node);
});

this.selectedItems = this.arr; // select all nodes
}

关于javascript - 如何选择素 Angular p树上的所有节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com