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
       
    },
相关推荐
速盾cdn31 分钟前
速盾:网页游戏部署高防服务器有什么优势?
服务器·前端·web安全
小白求学133 分钟前
CSS浮动
前端·css·css3
什么鬼昵称34 分钟前
Pikachu-csrf-CSRF(POST)
前端·csrf
golitter.1 小时前
Vue组件库Element-ui
前端·vue.js·ui
儒雅的烤地瓜1 小时前
JS | JS中判断数组的6种方法,你知道几个?
javascript·instanceof·判断数组·数组方法·isarray·isprototypeof
道爷我悟了1 小时前
Vue入门-指令学习-v-on
javascript·vue.js·学习
27669582921 小时前
京东e卡滑块 分析
java·javascript·python·node.js·go·滑块·京东
golitter.1 小时前
Ajax和axios简单用法
前端·ajax·okhttp
PleaSure乐事1 小时前
【Node.js】内置模块FileSystem的保姆级入门讲解
javascript·node.js·es6·filesystem
雷特IT2 小时前
Uncaught TypeError: 0 is not a function的解决方法
前端·javascript