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
       
    },
相关推荐
dal118网工任子仪3 分钟前
94,【2】buuctf web [安洵杯 2019]easy_serialize_php
android·前端·php
大模型铲屎官33 分钟前
HTML5 技术深度解读:本地存储与地理定位的最佳实践
前端·html·html5·本地存储·localstorage·地理定位·geolocation api
一 乐1 小时前
基于vue船运物流管理系统设计与实现(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端·船运系统
m0_528723811 小时前
在React中使用redux
前端·javascript·react.js
傻小胖2 小时前
vue3中customRef的用法以及使用场景
前端·javascript·vue.js
谦谦橘子2 小时前
手把手教你实现一个富文本
前端·javascript
Future_yzx2 小时前
Java Web的发展史与SpringMVC入门学习(SpringMVC框架入门案例)
java·前端·学习
star010-2 小时前
【视频+图文详解】HTML基础4-html标签的基本使用
前端·windows·经验分享·网络安全·html·html5
engchina2 小时前
CSS Display属性完全指南
前端·css
engchina2 小时前
详解CSS `clear` 属性及其各个选项
前端·css·css3