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
       
    },
相关推荐
陈_杨5 小时前
鸿蒙APP开发-带你走进胶片录的相机控制
前端·javascript
陈_杨5 小时前
鸿蒙APP开发-带你走进节流战的Canvas图表
前端·javascript
陈_杨5 小时前
鸿蒙APP开发-带你走进光绘记的拍摄规划
前端·javascript
陈_杨5 小时前
鸿蒙APP开发-带你走进光绘记的长曝光模拟
前端·javascript
陈_杨5 小时前
鸿蒙APP开发-带你走进节拍器的声音怎么这么准
前端·javascript
搬砖的阿wei5 小时前
Pinia 与 Vuex 区别
前端·vue.js
KaMeidebaby5 小时前
卡梅德生物技术快报|原核表达系统工艺优化:包涵体重折叠 + 分子筛纯化实现功能 RBD 高效制备,附全参数配置
前端·人工智能·算法·数据挖掘·数据分析
最爱睡觉睡觉睡觉5 小时前
代碼案例:CSS 屬性對照
前端·app
VitoChang6 小时前
开发体验超赞的SolidJS2.0来了
前端
CoCo的编程之路6 小时前
2026全栈演进:使用前端开发助手进行项目重构的最佳工具
大数据·前端·人工智能·ai编程·comate