mbraak / jqTree

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

Is it possible to append an element to an existent tree? #22

Closed rosenfeld closed 12 years ago

rosenfeld commented 12 years ago

I couldn't find any current API that would allow me to do that.

For example, in my application it is possible to create new elements in the tree dynamically.

Currently I'm refreshing the whole tree if I need to add another root element, while this also looses all information about collapsed and expanded nodes.

It would be handy if I could just append some element to the current tree. Is that already possible?

mbraak commented 12 years ago

You can use loadData to add a subtree to an existing tree.

var new_data = [
  label: 'new_element'
];
$('#tree1').tree('loadData', new_data, existing_node);
rosenfeld commented 12 years ago

Yeah, but how about adding a new root element?

mbraak commented 12 years ago

That's not possible at this moment. I guess it should be possible because drag-and-drop does also add nodes in every position in the tree. I will look into it.

rosenfeld commented 12 years ago

thanks :)

mbraak commented 12 years ago

The dev branch has new functions addNodeAfter, addNodeBefore and addParentNode.

To add a new parent to node 'node1':

$('#tree1').addParentNode(
    node1,
    {
        label: 'new_parent',
        id: 456
    }
);

By the way, for saveState to work, nodes in tree data must have an id field.

rosenfeld commented 12 years ago

Thank you very much, I'll give it a try as soon as I finish implementing another feature I'm working right now.

But I'm already closing this issue because I believe on you ;)

Cheers!

rosenfeld commented 12 years ago

By the way, I'm not using saveState because my tree is lazily evaluated... :)

rosenfeld commented 12 years ago

I've started to give this a try today and it seems addNodeAfter is working fine. I don't need addParentNode so I'll probably not test it, but I intend to test addNodeBefore later.

But I guess I'm missing something because I couldn't find out how to remove some node. Is there any removeNode method or another way of doing that? Or am I supposed to do something like $(tree.tree('getNodeById', id).element).remove()?

rosenfeld commented 12 years ago

I found removeChild, but how can I remove a root node?

rosenfeld commented 12 years ago

node.parent.removeChild(node) worked for me :)

mbraak commented 12 years ago

I will add a removeNode function

rosenfeld commented 12 years ago

thanks, @mbraak :)

mbraak commented 12 years ago

The removeNode function is now added to the dev branch

rosenfeld commented 12 years ago

thank you! I'll give it a try in a few minutes

rosenfeld commented 12 years ago

great! worked here, thanks! :)