ng2-tree icon indicating copy to clipboard operation
ng2-tree copied to clipboard

Reconstruct tree and access controller in same method (controller is null)

Open NewteqDeveloper opened this issue 5 years ago • 8 comments

We are having a problem with the getControllerByNodeId method on the treeComponent if we have recreated a tree dynamically and then want to access the controller to expand the nodes.

Basically, we do the following in a method

// create resultTree as TreeModel
this.tree = resultTree
const controller = this.treeComponent.getControllerByNodeId(lastNodeId)
controller.expandToParent()

The problem that we have is that the controller is null.

My guess is that it's because the treeComponent has not yet been updated with the new tree data when the call to the treeComponent happens.

I would like to know (because I can't seem to find it on the documentation) if there is a "callback" method that can be used for when the tree is updated in the treeComponent. I believe that this would cause the call to the getControllerByNodeId method to have the correct data

NewteqDeveloper avatar Aug 16 '18 15:08 NewteqDeveloper

I'm not sure how to apply the label - Help Wanted

NewteqDeveloper avatar Aug 16 '18 15:08 NewteqDeveloper

I actually tried the following:

// create resultTree as TreeModel
this.tree = resultTree
setTimeout(() => {
        const controller = this.treeComponent.getControllerByNodeId(lastNodeNumber);
        controller.expandToParent()
      }, 5000);

But the controller is still null. Any ideas?

NewteqDeveloper avatar Aug 16 '18 15:08 NewteqDeveloper

I'm having the same problem, is there a solution to this?

MickGyver avatar Sep 13 '18 11:09 MickGyver

I haven't found a solution to this yet. I think that some changes are required from the library

NewteqDeveloper avatar Sep 13 '18 11:09 NewteqDeveloper

@nfourie-work-profile @MickGyver

add keepNodesInDOM : true to settings.

vivekrao007 avatar Feb 05 '19 13:02 vivekrao007

I will give this a try and revert back if there is still a problem

NewteqDeveloper avatar Feb 11 '19 12:02 NewteqDeveloper

I'm having the same problem. Did you find a solution?

  handleExpandedEvent(event: NodeExpandedEvent) {
    const node = event.node;
    const controller = this.component.getControllerByNodeId(node.id);
    this.log.debug("node.id=",node.id, "; controller is null:", controller===null);
    this.dept.fetchSubordinatesOf(<number>node.id).subscribe(depts => {
      this.log.debug("controller is null:", controller===null);
    });
  }

kkebot avatar Mar 14 '19 01:03 kkebot

Unfortunately I have not yet been able to test the suggested approach in our current project. Once I get to test the suggested approach, I will revert back.

NewteqDeveloper avatar Mar 18 '19 08:03 NewteqDeveloper