antd vue tree的增删改和拖拽

最近项目中遇到一个tree型数据的的操作的功能,代码简单如下:

html 复制代码
 <a-tree
            showLine
            showIcon
            :draggable="draggable"
            :expandedKeys="expandedKeys"
            :treeData="treeData"
            @drop="onDrag"
            @expand="onExpand"
          >
             <template v-if="nodeBtns" slot="custom" slot-scope="item">
              <a-button size="small" icon="plus" @click="toAdd(item)" />
              <a-button size="small" icon="edit" @click="toEdit(item)" />
              <a-button size="small" icon="delete" @click="deleteTreeNode(item)" />
            </template>
            <a-spin :spinning="spinning" />
          </a-tree>

增加操作代码:

javascript 复制代码
   toAdd(item) {
      this.item={title:'', parentId:item.key}
      this.changeVisible=true // 这是弹窗,弹窗内输入title
    },
    treeAction (node, id, fn) {
      node.some((item,index) => {
        if (item.key === id) {
          fn(node, item, index)
          return true;
        }
        if (item.children && item.children.length) {
          this.treeAction(item.children, id, fn);
        }
      });
    },
    handleAdd(){
      let that=this
     
      ... // api操作
      // treeData数据的操作
                this.treeAction(this.treeData, this.item.parentId, (node, item, index) => {
                    item.children = item.children || [];
                    that.item.key=data // that.item为保存当前节点数据的变量
                    that.item.scopedSlots = { icon: "custom" }
                    item.children.push(that.item)     
                  });

       ...
    },

修改和添加类似,点击修改则弹窗修改窗体,然后确定则进行修改:

javascript 复制代码
 handleChange(){
    ... // api操作

    // treeData数据操作
     this.treeAction(this.treeData, this.item.parentId, (node, item, index) => {
                    item.children = item.children || [];
                    that.item.key=data
                    that.item.scopedSlots = { icon: "custom" }
                    item.children.push(that.item)     
                  });
    

删除操作:

javascript 复制代码
 delete(item) {
      let that = this;
      this.$confirm({
        title: "确认删除吗??",
        content: "点击确定删除",
        onOk() {
          ... // api操作
          // treeData操作
              that.treeAction(that.treeData, item.key, (node, item, index) => {
                node.splice(index, 1);
              });
        },
        onCancel() {},
      });
    },

拖拽操作:

javascript 复制代码
  onDrag(info){
      const node = info.node;
      const dragNode = info.dragNode;

        const dropKey = info.node.dataRef.key;
        const dragKey = info.dragNode.dataRef.key;
        const dropPos = info.node.pos.split('-');
        const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);

        const loop = (data, key, callback) => {
          data.forEach((item, index, arr) => {
            if (item.key === key) {
              return callback(item, index, arr);
            }
            if (item.children) {
              return loop(item.children, key, callback);
            }
          });
        };
        const data = [...this.treeData];

        let dragObj;
        loop(data, dragKey, (item, index, arr) => {
          arr.splice(index, 1);
          dragObj = item;
        });

        if (!info.dropToGap) {     
          loop(data, dropKey, item => {
            item.children = item.children || [];      
            item.children.push(dragObj);
          });
        } else {   
          let ar;
          let i;
          loop(data, dropKey, (item, index, arr) => {
            ar = arr;
            i = index;
          });
          if (dropPosition === -1) {   
            ar.splice(i, 0, dragObj);
          } else {
            ar.splice(i + 1, 0, dragObj);  
          }
        }
        ... // api
        // treeData重新赋值
        this.treeData= data
       
    },
相关推荐
淹死在鱼塘的程序猿12 分钟前
🚀 告别"一次性聊天":揭秘让 AI 智能体越用越聪明的秘密武器 —— Skills
前端·人工智能·agent
掘金安东尼16 分钟前
OpenMUSE 全面详解:非扩散Transformer文生图开源基座(对标GPT Image 2)
前端·javascript·面试
~ rainbow~25 分钟前
前端转型全栈(六)——深入浅出:文件上传的原理与进阶
前端·http·文件上传
我就是马云飞1 小时前
我废了!大厂10年的我面了20家公司,面试官让我回去等通知!
android·前端·程序员
yizhiyang1 小时前
ECharts实战:滑动缩放+选中背景高亮,打造高颜值统计图表
前端
猫山月1 小时前
Flutter路由演进路线(2026)
前端·flutter
We་ct1 小时前
LeetCode 322. 零钱兑换:动态规划入门实战
前端·算法·leetcode·typescript·动态规划
_白_1 小时前
从 0 到上线:我如何用开源打造一款密码管理 Chrome 插件
javascript
袋鱼不重1 小时前
Hermes Agent 直连飞书机器人
前端·后端·ai编程
不务正业的前端学徒1 小时前
Threejs,地图标签绘制,碰撞检测逻辑
前端