vuejs-tree
vuejs-tree copied to clipboard
delete node sample bug fix suggestion
suggestion for sample, https://codesandbox.io/s/vuejs-tree-sandbox-v3x-lmbyx
deleteNodeFunction: function (node) {
const nodePath = this.$refs["my-tree"].findNodePath(node.id);
const parentNodeId = nodePath.slice(-2, -1)[0];
if (parentNodeId === undefined) {
// 'root' node
const nodeIndex =
this.$refs["my-tree"].nodes.findIndex((x) => x.id !== node.id) - 1; **//this return -1 when list delete center. only work on end of nodes**
this.$refs["my-tree"].nodes.splice(nodeIndex, 1);**//when nodeIndex -1, this delete last of nodes always, not select one**
} else {
// child node
const parentNode = this.$refs["my-tree"].findNode(parentNodeId);
const nodeIndex =
parentNode.nodes.findIndex((x) => x.id !== node.id) - 1; **//this return -1 when list delete center. only work on end of nodes**
parentNode.nodes.splice(nodeIndex, 1); **//when nodeIndex -1, this delete last of nodes always, , not select one**
}
console.log("example: remove node", node.id);
},
------------------- my fix suggestion -----------------------
deleteNodeFunction: function (node) {
let nodePath = this.$refs["my-tree"].findNodePath(node.id);
let parentNodeId = nodePath.slice(-2, -1)[0];
if (parentNodeId === undefined) {
// 'root' node
console.log("root node delete");
**let nodeIndex = this.$refs["my-tree"].nodes.findIndex((x) => x.id == node.id);**
if (nodeIndex >= 0) {
this.$refs["my-tree"].nodes.splice(nodeIndex, 1);
} else {
console.warn("root node delete - delete index < 0");
}
} else {
// child node
console.log("child node delete");
let parentNode = this.$refs["my-tree"].findNode(parentNodeId);
**let nodeIndex = parentNode.nodes.findIndex((x) => x.id == node.id);**
if (nodeIndex >= 0) {
parentNode.nodes.splice(nodeIndex, 1);
} else {
console.warn("child node delete - delete index < 0");
}
}
console.log("example: remove node", node.id);
},