Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete node sample bug fix suggestion #65

Open
sangjela opened this issue Jan 30, 2023 · 0 comments
Open

delete node sample bug fix suggestion #65

sangjela opened this issue Jan 30, 2023 · 0 comments

Comments

@sangjela
Copy link

sangjela commented Jan 30, 2023

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);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant