mbraak/jqTree

Cannot find node with getNodeById

Closed this issue · 12 comments

I am facing a problem, that might be my silly mistake, but i cannot figure it out.
I have a tree that reloads its data after a selection. I want to expand all the parent nodes of a specific node, but after loading data ( i use tree.load_data event) i tried to get the node by using node = $('myTtree').tree('getNodeById', $('selection').val()); but every time i get a node = null. I even tried to hardcopy the id of the node, but same.

Below is the code i am using. Please any help would be appreciated

$('#myTree').on( 'tree.load_data', function(e) { if((e.tree_data).length === 1){ var elem = e.tree_data[0] while(elem.hasOwnProperty('children') && (elem.children).length === 1){ var node = $('#myTree').tree("getNodeById", $('#selection').val() ); console.log(node); //this is always null for some reason elem = elem.children[0]; } } } );

When the tree.load_data is event is sent, the tree is not yet initialized. That is the reason that getNodeByd doesn't work.

I'll have a look if it's possible to change this.

I was unable to find a way to manually open nodes after onCreateLi, where i use to customize each node. So the only way to do this was foreach node, inside onCreateLi, to add/remove css classes so to open the current node when required. But it would be great if there was an event to call after all initialization was done, including the customization of the nodes.

I'm of thinking of adding a tree.reinit event, that is called when the whole tree is reloaded. You could then listen to the tree.init and tree.reinit events if you want to handle a tree reload.

  • tree.init: called when the tree is initialized; this event is called only once.
  • tree.reinit: called when the tree is re-initialized;

Background: I cannot call tree.init on tree reload because this breaks existing code.

It's implemented in this pr: #705. It's a work in progress, but it does work.

NB: this only works if you reload the entire tree.

Would this work for in your situation?

well i'm not sure. What i actually want to achieve is that after loadDataFromUrl, i customize each node of the tree to add a custom button, using onCreateLi. After customizing all the nodes, i want to parse each node of the tree, and open some nodes i choose. My current problemt is that i cannot find an event to check wheter the onCreateLi has finished, and all nodes are visible, so to parse each node individualy.

When you call loadDataFromUrl, are you reloading the whole tree, or only part of the tree?

Background: it's possible to load part of the tree by using the parentNode parameter. https://mbraak.github.io/jqTree/#functions-loaddatafromurl

In my case i need to reload the whole tree, thats why everytime i have to customize the nodes. I think if there was an event like "onDrawFinished" which is fired after all the nodes are draw, it would be the best.

I updated pr #705. It now triggers the tree.load_data event after the tree is (reloaded and) redrawn.

I think this should work with your original code that handles the tree.load_data event.

@yakamuki Can I ask you to test if the #705 pull request fixes the issue? The tree.load_data event is triggered after drawing the tree, so the getNodeById call should work.

@mbraak i'll give it a try now. I'm currently working on a project and installed the library via "npm"

@mbraak it works great. The tree.load_data event is triggered after the whole tree is drawn, so now i can manipulate all the nodes i need. Thanks

Thanks for testing! I'll merge the pr.

Version 1.7.0 is just published, including this fix.