patternfly-bootstrap-treeview
patternfly-bootstrap-treeview copied to clipboard
Programmatically expanding a node from onNodeSelected event isn't working
Not sure what I'm doing wrong. I'm trying to programmatically expand a node when the node is clicked (selected). The tree visually expands, but when I get a list of all the expanded nodes, the list is empty.
Please see the jsfiddle https://jsfiddle.net/acapkxhn/3/ Steps:
- open a javascript console to see the log messages
- click on the Parent 1 node The tree expands, but the list returned from tree.getExpanded() is empty.
I'm using
- jquery 3.2.1
- bootstrap 3.3.7
- patternfly-bootstrap-treeview 2.1.5
Any idea what I'm doing wrong or not doing?
Hi, I am also facing same issue. Any solution on this ?
@ashishdeole, Sorry, I never did find a solution. What I ended up doing was using a different treeview control.
@ErnieAllen and @ashishdeole the string concatenation in the demo works strange but if the event did not finish while running the callback, wrapping it around a setTimeout solves the issue:
var onTreeNodeSelected = function(x,y) {
setTimeout(function() {
// here goes your code
});
};
@skateman and @ashishdeole I updated the fiddle to call tree.expandNode(node) in a setTimeout. This didn't fix the problem.
The list returned from tree.getExpanded() is still empty. I also tried wrapping the code in the onTreeNodeExpanded event handler in a timeout, but that didn't work either.
Here is the updated fiddle with the setTimeouts. https://jsfiddle.net/acapkxhn/6/
Hm, strange ... it works for me.

Also you don't need the second argument for the setTimeout, it's enough to put the function on the end of the execution queue.
Sorry I wasn't clear in my description. When I click on the +, the tree is expanded automatically and the onTreeeNodeExpanded event hander is able to get the expanded list.
However, if I click on the text 'Parent 1', that is when the onTreeNodeSelected event is fired and I'm programmatically expanding the tree. In that case I'm not seeing the correct expanded list.
@ErnieAllen strange, the workaround could be to set the expansion to silent and call the handler manually.
@skateman I did what I think you suggested and no joy. https://jsfiddle.net/acapkxhn/7/ An extract of the pertinent part: tree.expandNode(node, {silent: true}) setTimeout( function () { onTreeNodeExpanded(e, node) }) The list returned by tree.getExpanded() is still empty after calling tree.expandNode(..)
I will take a look into this after Christmas...
Any updates please ?