patternfly-bootstrap-treeview
patternfly-bootstrap-treeview copied to clipboard
Adding child node to the node returned from `onNodeSelected` fails
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);
},
// ...
}
Any solution for this?
$('#tree').treeview(true).addNode(nodes=s_nodes,$('#tree').treeview('getSelected'));
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.