Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Adding child node to the node returned from onNodeSelected fails #60

Open
rogertangcn opened this issue Nov 23, 2017 · 3 comments
Open

Comments

@rogertangcn
Copy link

rogertangcn commented Nov 23, 2017

I was trying to use the node returned from event hooks (e.g. onNodeSelected(event, node)) as parent and to add a child to that parent without success. Here is an emulating code of what i was doing.

$("#tree").treeview({
  // ...
   onNodeSelected: function (event, node) {
      var folder = {
        name: "child",
        // ...
      };
      $("#tree").treeview(true).addNode(folder, node);
  },
// ...
}

The cause of that is the node returned from the hook is a deep copy. See below code

        // ...
        this._triggerEvent('nodeSelected', node, options);
        // ...
       Tree.prototype._triggerEvent = function (event, data, options) {
		if (options && !options.silent) {
                        // $.extend(true, {}, data) is a deep copy of the original node.
			this.$element.trigger(event, $.extend(true, {}, data));
		}
	}   

So, adding child is to add child to the copy which has no effect to the tree.

The issue can be worked around by getting the original node (see below). Wound it be better to return the original copy? Is it intentional to return a replicate over original to avoid internal data structure being messed up? Thanks.

$("#tree").treeview({
  // ...
   onNodeSelected: function (event, node) {
      var folder = {
        name: "child",
        // ...
      };
      var parent = $("#tree").treeview(true).findNodes(node.id, "id");
      $("#tree").treeview(true).addNode(folder, parent);
  },
  // ...
}
@dgroh
Copy link

dgroh commented May 16, 2018

Any solution for this?

@kaaxsh
Copy link

kaaxsh commented Jul 10, 2018

$('#tree').treeview(true).addNode(nodes=s_nodes,$('#tree').treeview('getSelected'));

@ArthurFreitas
Copy link

ArthurFreitas commented Aug 28, 2019

I was facing the same issue while trying to unselect the deep copied node inside the "onNodeSelected" event. The 'getSelected' method kept returning the unselected node even though the 'selected' class was removed from the html element.

@rogertangcn workaround worked, ty.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants