question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set isExpanded status of added node to true

See original GitHub issue

Hey there,

I created a function that adds a node, updates the tree, does everything correctly, except by default it seems to want to not be expanded. If I try to add other nodes inside of that node, the nodes do not show up because the node is not expanded. I thought if I set isExpanded: true, then the node would automatically be expanded. I’m using angular2-tree-component 2.6.1.

Here is my function:

addSection() {
    this.items.push({
      id: 0,
      isExpanded: true,
      isSection: true,
      expanded: false,
      name: '[Section name needed]',
      children: []
    });

    // Update the tree model to reflect changes
    this.tree.treeModel.update();
  }

Do you have any thoughts?

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
adamkleingitcommented, Feb 2, 2017

Hey guys, I’m glad you solved this on your own. I was on a long sickness leave and couldn’t help.

The solution is what was proposed by @romanovma .

  • change data
  • call update()
  • locate the node (by id, path, whatever)
  • call methods on it

Basically isExpanded field is a tricky one. I only look at that field the first time the tree is rendered, or whenever async children are loaded. The state of expanded nodes is saved inside the treeModel, and not on the nodes themselves. I’m trying to avoid 2-way binding to properties of the nodes. It is not good practice to change the data that was given to the tree.

1reaction
romanovmacommented, Jan 30, 2017

@snstarosciak you should update the tree model before you use its methods which means that first run:

// Update the tree model to reflect changes
    this.list.items=[].concat(this.list.items);

and then

let createdNode: TreeNode = this.tree.treeModel.getNodeById(item.id);
    // Log to the node to see if it exists
    console.log('createdNode', createdNode);
    
    createdNode.expand();

hope that helps

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set isExpanded status of added node to true #141
I'm using angular2-tree-component 2.6.1 . Here is my function: addSection() { this.items.push({ id: 0, isExpanded: true, isSection ...
Read more >
c# - Setting IsExpanded on a WPF TreeViewItem from ...
There are a few things wrong here. The first is that you are setting the property TreeViewItem.IsSelected on a HierarchicalDataTemplate .
Read more >
How to programmatically expand a tree view control? - MSDN
I want to programmatically expand all the nodes on a tree view control. ... I suggest when you set 'IsExpanded = true' then...
Read more >
Need to programmatically expand tree node if a child ...
I tried adding an IsExpanded property to my model class and ... and then setting IsExpanded=true for the parent node meeting my criteria....
Read more >
bind property to IsExpanded of XamGridTree in TwoWay
Another 50 percent is how to make XamTreeGrid actually expand node when IsOpen is set to true upon binding? To reproduce, assign value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found