实现 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>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值