如何初始化primeng树组件
how to initialize primeng tree component
给定一个tree
如何初始化节点随意展开?
我已经尝试通过 @ViewChildren(Tree) tree
获取引用,但在尝试访问他的 children
时导致了 undefined
引用
这是一个基本上模拟沿着树点击的 hack。我添加了这个解决方案,但我真的希望有人能找到更好的东西。
给定一个带有 tree
的组件,我们可以获得对 treenodes
的引用,然后根据需要 "click" 它们:
@Component({
selector: 'filemanager',
templateUrl: './filemanager.html',
directives: [Tree]
})
export class FileManagerComponent implements AfterViewInit {
constructor(private renderer:Renderer) {}
ngAfterViewInit() {
setTimeout(() => { // a timeout is necessary otherwise won't find the elements
// get the first "p-tree" tag and find his first "toggler"
let element = document.getElementsByTagName("p-tree")[0].getElementsByClassName("ui-tree-toggler fa fa-fw fa-caret-right")[0];
//"click" the toggler using the angular2 renderer
let event = new MouseEvent('click', {bubbles: true});
this.renderer.invokeElementMethod(element, 'dispatchEvent', [event]);
}, 200);
}
// more methods and state...
}
如果您需要初始化树中更深的节点,您将需要嵌套 setTimeout
函数。
要初始化扩展的树组件,您只需要在 json 中设置 属性 扩展为 true 的格式。
样本:
{
"data":
[
{
"label": "Pictures",
"data": "Pictures Folder",
"expandedIcon": "fa-folder-open",
"collapsedIcon": "fa-folder",
"expanded": true, // this flag shoud be true
"children": [
{"label": "barcelona.jpg", "icon": "fa-file-image-o", "data": "Barcelona Photo"},
{"label": "logo.jpg", "icon": "fa-file-image-o", "data": "PrimeFaces Logo"},
{"label": "primeui.png", "icon": "fa-file-image-o", "data": "PrimeUI Logo"}]
},
]
}
您可以使用将所有扩展属性设置为 true 的函数来处理此问题。
expandAll(toggle: boolean) {
this.tree.map(node => {
node.expanded = toggle;
});
}
ngOnInit() {
setTimeout(()=>{
this.expandAll(true);
}, 0);
}
给定一个tree
如何初始化节点随意展开?
我已经尝试通过 @ViewChildren(Tree) tree
获取引用,但在尝试访问他的 children
undefined
引用
这是一个基本上模拟沿着树点击的 hack。我添加了这个解决方案,但我真的希望有人能找到更好的东西。
给定一个带有 tree
的组件,我们可以获得对 treenodes
的引用,然后根据需要 "click" 它们:
@Component({
selector: 'filemanager',
templateUrl: './filemanager.html',
directives: [Tree]
})
export class FileManagerComponent implements AfterViewInit {
constructor(private renderer:Renderer) {}
ngAfterViewInit() {
setTimeout(() => { // a timeout is necessary otherwise won't find the elements
// get the first "p-tree" tag and find his first "toggler"
let element = document.getElementsByTagName("p-tree")[0].getElementsByClassName("ui-tree-toggler fa fa-fw fa-caret-right")[0];
//"click" the toggler using the angular2 renderer
let event = new MouseEvent('click', {bubbles: true});
this.renderer.invokeElementMethod(element, 'dispatchEvent', [event]);
}, 200);
}
// more methods and state...
}
如果您需要初始化树中更深的节点,您将需要嵌套 setTimeout
函数。
要初始化扩展的树组件,您只需要在 json 中设置 属性 扩展为 true 的格式。
样本:
{
"data":
[
{
"label": "Pictures",
"data": "Pictures Folder",
"expandedIcon": "fa-folder-open",
"collapsedIcon": "fa-folder",
"expanded": true, // this flag shoud be true
"children": [
{"label": "barcelona.jpg", "icon": "fa-file-image-o", "data": "Barcelona Photo"},
{"label": "logo.jpg", "icon": "fa-file-image-o", "data": "PrimeFaces Logo"},
{"label": "primeui.png", "icon": "fa-file-image-o", "data": "PrimeUI Logo"}]
},
]
}
您可以使用将所有扩展属性设置为 true 的函数来处理此问题。
expandAll(toggle: boolean) {
this.tree.map(node => {
node.expanded = toggle;
});
}
ngOnInit() {
setTimeout(()=>{
this.expandAll(true);
}, 0);
}