vue封装一个有外部按钮新增同级和下级,修改,能编辑节点内容的el-tree组件

该文章展示了一个Vue组件,该组件封装了el-tree,功能包括通过外部按钮新增节点(同级或下级)、编辑节点内容。点击按钮触发不同的方法,如addSibling用于新增同级节点,addChild用于新增下级节点,editNode用于编辑节点,saveNode用于保存编辑后的节点内容。用户可通过节点点击事件获取并更新当前选中节点。
摘要由CSDN通过智能技术生成

vue封装一个有外部按钮新增同级和下级,修改,能编辑节点内容的el-tree组件

<template>
  <div>
    <el-button type="primary" @click="addSibling">新增同级</el-button>
    <el-button type="primary" @click="addChild">新增下级</el-button>
    <el-button type="primary" @click="editNode">修改</el-button>
    <el-tree :data="treeData" :props="defaultProps">
      <span class="custom-tree-node" slot-scope="{ node, data }">{{ data.label }}</span>
    </el-tree>
    <el-dialog v-model="dialogVisible">
      <el-form ref="nodeForm" :model="nodeForm" label-width="80px">
        <el-form-item label="节点名称">
          <el-input v-model="nodeForm.label"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveNode">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'EditableTree',
  props: {
    treeData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      dialogVisible: false,
      nodeForm: {
        label: ''
      },
      currentNode: null
    }
  },
  methods: {
    addSibling() {
      // 获取当前节点的父节点
      const parent = this.currentNode.parent;
      // 在父节点的children数组中添加一个新节点
      parent.children.push({
        label: '新同级节点'
      });
    },
    addChild() {
      this.currentNode.children.push({
        label: '新下级节点'
      });
    },
    editNode() {
      this.nodeForm.label = this.currentNode.label;
      this.dialogVisible = true;
    },
    saveNode() {
      this.currentNode.label = this.nodeForm.label;
      this.dialogVisible = false;
    },
    handleNodeClick(data, node) {
      this.currentNode = node;
    }
  }
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值