2

给定tree如何以随意扩展节点的方式对其进行初始化?

我已经尝试获得参考,但在尝试访问他的孩子时@ViewChildren(Tree) tree导致参考undefined

4

3 回答 3

3

这是一个 hack,基本上模拟了树上的点击。我添加了这个解决方案,但我真的希望有人能找到更好的东西。

给定一个带有 a 的组件,tree我们可以获得对 的引用,treenodes然后根据需要“单击”它们:

@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函数。

于 2016-07-09T19:31:46.567 回答
1

要初始化扩展的树组件,您只需要以 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"}]
        },
    ]
}
于 2017-04-06T17:26:16.097 回答
1

您可以使用将所有扩展属性设置为 true 的函数来处理此问题。

expandAll(toggle: boolean) {
  this.tree.map(node => {
    node.expanded = toggle;
  });
}

ngOnInit() {
  setTimeout(()=>{
    this.expandAll(true);
  }, 0);

}
于 2017-10-31T22:34:48.060 回答