当前位置:   article > 正文

实现 el-tree 自定义节点的新增、修改和删除_el-tree添加节点

el-tree添加节点

在网上找了好多代码,总是会有各种问题,成功只能靠自己呀

<template>
  <div>
    <el-tree
      :data="treeData"
      :props="treeProps"
      :default-expand-all="true"
      :render-content="renderContent"
    ></el-tree>
  </div>
</template>

<script>
export default {
  data() {
    return {
      treeData: [
        {
          id: 1,
          label: "Node 1",
          children: [
            {
              id: 2,
              label: "Node 1-1",
            },
            {
              id: 3,
              label: "Node 1-2",
            },
          ],
        },
        {
          id: 4,
          label: "Node 2",
        },
      ],
      treeProps: {
        children: "children",
        label: "label",
      },
    };
  },
  methods: {
    renderContent(h, { node, data }) {
      return (
        <span class="custom-node">
          {node.label}
          <span class="node-actions">
            <el-button
              type="text"
              icon="el-icon-plus"
              onClick={() => this.addNode(data)}
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-edit"
              onClick={() => this.editNode(data)}
            ></el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              onClick={() => this.deleteNode(data)}
            ></el-button>
          </span>
        </span>
      );
    },
    addNode(nodeData) {
      const newNode = {
        id: Date.now(),
        label: "New Node",
      };
      if (!nodeData.children) {
        this.$set(nodeData, "children", []);
      }
      nodeData.children.push(newNode);
    },
    editNode(nodeData) {
      this.$prompt("Enter new label", "Edit Node", {
        inputValue: nodeData.label,
        confirmButtonText: "Save",
        cancelButtonText: "Cancel",
      })
        .then(({ value }) => {
          nodeData.label = value;
        })
        .catch(() => {});
    },
    deleteNode(nodeData) {
      this.$confirm("Are you sure to delete this node?", "Delete Node", {
        confirmButtonText: "Delete",
        cancelButtonText: "Cancel",
        type: "warning",
      })
        .then(() => {
          const parent = this.findParent(this.treeData, nodeData.id);
          parent.children = parent.children.filter(
            (child) => child.id !== nodeData.id
          );
        })
        .catch(() => {});
    },
    findParent(tree, nodeId) {
      for (let node of tree) {
        if (node.children) {
          if (node.children.some((child) => child.id === nodeId)) {
            return node;
          } else {
            const parent = this.findParent(node.children, nodeId);
            if (parent) {
              return parent;
            }
          }
        }
      }
      return null;
    },
  },
};
</script>

<style scoped>
.custom-node {
  display: flex;
  align-items: center;
}

.node-actions {
  margin-left: auto;
}
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/528357
推荐阅读
相关标签
  

闽ICP备14008679号