mbraak / jqTree

Tree widget for jQuery
https://mbraak.github.io/jqTree/
Apache License 2.0
1.02k stars 177 forks source link

Best way to get index of node? #673

Closed WriterStat closed 2 years ago

WriterStat commented 2 years ago

Hey Marco,

Best way to get index of node?

Thanks!

mbraak commented 2 years ago

I don't understand what you mean with the 'index'. Do you mean the id?

WriterStat commented 2 years ago

Hi Marco, I'm going to try using:

var np = node.parent; const pind = np.parent.getChildIndex(node); const eind = node.parent.getChildIndex(node); console.log('pind: '+pind); console.log('eIind: '+eind); To test...

Index in object for the parent. Index in the parent's children for the current node.

WriterStat commented 2 years ago

Not sure if I have that quite right. But will figure it out better when I test.

WriterStat commented 2 years ago

Figured it out, In case this helps anyone, but we may be the only one who needed this =) :

var node = $tree.tree("getNodeById", nid);
if (node) { 'const np = node.parent; const parent_index = np.parent.getChildIndex(np); const child_index = node.parent.getChildIndex(node); console.log('parent_index: '+parent_index); console.log('child_index: '+child_index); }`

We pass around our own internal array of objects(2 levels) which corresponds in some ways to the jqTree one. When changes happen in the jqTree having the parent and child index allows us to then update our own internal array automatically.

Cheers, I should have tested first. Will close.