patternfly-bootstrap-treeview icon indicating copy to clipboard operation
patternfly-bootstrap-treeview copied to clipboard

Adding child node to the node returned from `onNodeSelected` fails

Open rogertangcn opened this issue 7 years ago • 3 comments

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);
  },
  // ...
}

rogertangcn avatar Nov 23 '17 22:11 rogertangcn

Any solution for this?

dgroh avatar May 16 '18 21:05 dgroh

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

kaaxsh avatar Jul 10 '18 16:07 kaaxsh

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.

ArthurFreitas avatar Aug 28 '19 14:08 ArthurFreitas