0

I want to expand the first node on startup. I saw here the way to go. The difference is that I don't have the nodes in my code but get it from my databse. This is my Code:

this.appService.getTreeStructure().then((treeNodes) => {
      this.nodes = treeNodes;
      const treeModel: TreeModel = this.treeComponent.treeModel;
      console.log(treeModel);
      const firstNode: TreeNode = treeModel.getFirstRoot();
      console.log(firstNode);
      firstNode.expand();
    });

That doesnt work. the first console log shows that treeModel is an object but the second one is undefined. I can the a nodes property in treeModel that has allready all the nodes in it from the first output but I cant get access to them.

How can I expande the first node on startup?

LastChar23
  • 449
  • 1
  • 4
  • 15
  • Can you recreate this in a stackblitz? There's not enough information to go on here. – Kurt Hamilton Feb 11 '20 at 08:02
  • I dont know About stackblitz. But I just make a normal tree with angular tree component with node binding to `nodes`. The Code in my question is located in the 'ngOnInit' function. – LastChar23 Feb 11 '20 at 08:44
  • Google is your friend: https://stackblitz.com/. It's a way for you to create a live example of your problem so that people who want to help you can easily see what's going on without guessing at what might be wrong with code that isn't given in the question. – Kurt Hamilton Feb 11 '20 at 09:11

1 Answers1

2

When you update your nodes you need to listen to the updateData Event.

<tree-root [nodes]="nodes" (updateData)="onUpdateData()"></tree-root>

onUpdateData() {
      const treeModel: TreeModel = this.treeComponent.treeModel;
      const firstNode: TreeNode = treeModel.getFirstRoot();
      firstNode.expand();
}
sirzento
  • 537
  • 1
  • 5
  • 23