vue elementui tree 任意级别拖拽功能代码


Posted in Javascript onAugust 31, 2020

我的是根据父级id做的一些判断

<el-tree
     draggable :allow-drop="allowDrop" @node-drop="sort"
     accordion style="font-size:14px;width:250px;"
     ref="tree" :data="catalogList" :props="defaultProps" :expand-on-click-node="false"
     node-key="id" :highlight-current="true" :load="loadNode"
     lazy :render-content="renderContent" @node-click="handleNodeClick"
     empty-text="暂无数据">
 
   allowDrop(draggingNode, dropNode, type){
   //注掉的是同级拖拽
   /* if (draggingNode.data.level === dropNode.data.level) {
    if (draggingNode.data.aboveId === dropNode.data.aboveId) {
     return type === 'prev' || type === 'next'
    }
   } else {
    // 不同级进行处理
    return false
   } */
   //任意级别拖拽
   if (draggingNode.data.aboveId === dropNode.data.aboveId) {
     return type === 'prev' || type === 'next'
   } else {
    return type === 'prev' || type === 'next' || type === 'inner'
   }
  },
  //拖拽完成之后要重新排序
  /* 
  * draggingNode:被拖拽节点对应的 Node
  * dropNode:结束拖拽时最后进入的节点
  * type: 被拖拽节点的放置位置(before、after、inner)
  * event
  */
  sort(draggingNode,dropNode,type,event) {
   console.log(draggingNode)
   console.log(dropNode)
   if (draggingNode.data.aboveId === dropNode.data.aboveId) {
    let obj = {
     aboveId:'',
     arr:[]
    }
    obj.aboveId = dropNode.data.aboveId
    for (let item of dropNode.parent.childNodes) {
     obj.arr.push(item.data.id)
    }
    console.log(obj)
    this.updateOrderMe(obj)
   } else {
    let obj = {
     aboveId:'',
     id:'',
     newAboveId:''
    }
    obj.aboveId = draggingNode.data.aboveId
    obj.id = draggingNode.data.id
    obj.newAboveId = dropNode.data.id
    this.randomDrag(obj)
   }
  },
  randomDrag(obj) {
   this.$http
    .post(url, obj).then(res =>{
	    if (!res.data.success) {
	     this.$message.warning(res.data.msg)
	    }
    })
  },
  updateOrderMe(obj) {
   this.$http
    .post(url, {
     aboveId:obj.aboveId,
     ids: obj.arr
    }).then(res =>{
	    if (!res.data.success) {
	     this.$message.warning(res.data.msg)
	    }
    })
  }

补充知识:element-ui tree 实现同级拖拽

我就废话不多说了,大家还是直接看代码吧~

<template>
 <div>
  <el-tree
   draggable
   :allow-drop="allowDrop"
   @node-drop="sort"
   ref="tree"
   :data="data2"
   :props="defaultProps"
   show-checkbox
   default-expand-all
   node-key="id"
   highlight-current
  ></el-tree>
 
  <div class="buttons">
   <el-button @click="getCheckedNodes">通过 node 获取</el-button>
   <el-button @click="getCheckedKeys">通过 key 获取</el-button>
   <el-button @click="setCheckedNodes">通过 node 设置</el-button>
   <el-button @click="setCheckedKeys">通过 key 设置</el-button>
   <el-button @click="resetChecked">清空</el-button>
  </div>
 </div>
</template>
 
<script>
// import draggable from "vuedraggable";
// import Sortable from "sortablejs";
export default {
 methods: {
  getCheckedNodes() {
   console.log(this.$refs.tree.getCheckedNodes());
  },
  getCheckedKeys() {
   console.log(this.$refs.tree.getCheckedKeys());
  },
  setCheckedNodes() {
   this.$refs.tree.setCheckedNodes([
    {
     id: 5,
     label: "二级 2-1"
    },
    {
     id: 9,
     label: "三级 1-1-1"
    }
   ]);
  },
  setCheckedKeys() {
   this.$refs.tree.setCheckedKeys([3]);
  },
  resetChecked() {
   this.$refs.tree.setCheckedKeys([]);
  }
 },
 mounted() {
  const el = document.querySelectorAll(".el-tree")[0];
  console.log(el);
 },
 data() {
  return {
   data2: [
    {
     id: 1,
     label: "一级 1",
     children: [
      {
       id: 4,
       label: "二级 1-1",
       prop: "4"
      }
     ]
    },
    {
     id: 2,
     label: "一级 2",
     children: [
      {
       id: 5,
       label: "二级 2-1",
       prop: "5"
      },
      {
       id: 6,
       label: "二级 2-2",
       prop: "6"
      }
     ]
    },
    {
     id: 3,
     label: "一级 3",
     children: [
      {
       id: 7,
       label: "二级 3-1",
       prop: "7"
      },
      {
       id: 8,
       label: "二级 3-2",
       prop: "9"
      }
     ]
    },
    {
     id: 9,
     label: "一级4"
    }
   ],
   defaultProps: {
    children: "children",
    label: "label"
   },
   allowDrop(draggingNode, dropNode, type) {
    if (draggingNode.level === dropNode.level) {
     if (draggingNode.parent.id === dropNode.parent.id) {
      // 向上拖拽 || 向下拖拽
      return type === "prev" || type === "next";
     }
    } else {
     // 不同级进行处理
     return false;
    }
   },
   sort(draggingNode, dropNode, type, event) {
    // console.log('排序')
    // console.log("<><><>>><><<><><><><><><><>")
    // 拖拽之后的重新组合的数组
    // console.log(dropNode.parent); //dropNode.parent.childNodes =[]
    let obj = {
     aboveId: "",
     arr: []
    };
    obj.aboveId = dropNode.data.aboveId;
    for (let item of dropNode.parent.childNodes) {
     obj.arr.push(item.data.id);
    }
   }
  };
 }
};
</script>

以上这篇vue elementui tree 任意级别拖拽功能代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
符合标准的js表单提交的代码
Sep 13 Javascript
javascript 有用的脚本函数
May 07 Javascript
学习面向对象之面向对象的基本概念:对象和其他基本要素
Nov 30 Javascript
javascript日期转换 时间戳转日期格式
Nov 05 Javascript
fancybox modal的完美解决(右上的X)
Oct 30 Javascript
深入理解JavaScript系列(44):设计模式之桥接模式详解
Mar 04 Javascript
基于jquery实现表格内容筛选功能实例解析
May 09 Javascript
JS实现添加,替换,删除节点元素的方法
Jun 30 Javascript
H5移动端适配 Flexible方案
Oct 24 Javascript
设置jquery UI 控件的大小方法
Dec 12 Javascript
详解vue-cli@2.x项目迁移日志
Jun 06 Javascript
javascript实现扫雷简易版
Aug 18 Javascript
Element-ui树形控件el-tree自定义增删改和局部刷新及懒加载操作
Aug 31 #Javascript
JS遍历树层级关系实现原理解析
Aug 31 #Javascript
Element-ui el-tree新增和删除节点后如何刷新tree的实例
Aug 31 #Javascript
Vue循环中多个input绑定指定v-model实例
Aug 31 #Javascript
浅析 Vue 3.0 的组装式 API(一)
Aug 31 #Javascript
vue中v-model对select的绑定操作
Aug 31 #Javascript
Vue v-for中的 input 或 select的值发生改变时触发事件操作
Aug 31 #Javascript
You might like
php实现使用正则将文本中的网址转换成链接标签
2014/12/03 PHP
Zend Framework动作助手Url用法详解
2016/03/05 PHP
基于PHP实现解密或加密Cloudflar邮箱保护
2020/06/24 PHP
firefo xml 读写实现js代码
2009/06/11 Javascript
jqgrid 编辑添加功能详细解析
2013/11/08 Javascript
Bootstrap缩略图的创建方法
2017/03/22 Javascript
React实践之Tree组件的使用方法
2017/09/30 Javascript
浅谈 vue 中的 watcher
2017/12/04 Javascript
JS实现进度条动态加载特效
2020/03/25 Javascript
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
2020/04/30 jQuery
vue实现输入框自动跳转功能
2020/05/20 Javascript
[51:34]Ti4主赛事胜者组 DK vs EG 2
2014/07/19 DOTA
仅用50行Python代码实现一个简单的代理服务器
2015/04/08 Python
TensorFlow实现Batch Normalization
2018/03/08 Python
python 定时器,轮询定时器的实例
2019/02/20 Python
详解python爬虫系列之初识爬虫
2019/04/06 Python
Python测试Kafka集群(pykafka)实例
2019/12/23 Python
Django实现列表页商品数据返回教程
2020/04/03 Python
python模拟哔哩哔哩滑块登入验证的实现
2020/04/24 Python
French Connection官网:女装、男装及家居用品
2019/03/18 全球购物
Goodee官方商店:迷你投影仪
2021/03/15 全球购物
Linux管理员面试经常问道的相关命令
2014/12/12 面试题
造型师求职自荐信
2013/09/27 职场文书
财务管理专业推荐信
2013/11/19 职场文书
安全大检查实施方案
2014/02/22 职场文书
我爱读书演讲稿
2014/05/07 职场文书
交警个人先进事迹材料
2014/05/11 职场文书
本科应届生求职信
2014/08/05 职场文书
学校创先争优活动总结
2014/08/28 职场文书
教师党员自我剖析材料
2014/09/29 职场文书
上班迟到检讨书范文300字
2014/11/02 职场文书
涪陵白鹤梁导游词
2015/02/09 职场文书
教师工作态度自我评价
2015/03/05 职场文书
教师教育教学随笔
2015/08/15 职场文书
2019班干部竞选演讲稿范本!
2019/07/08 职场文书
Win11怎样将锁屏账户头像图片改成动画视频
2021/11/21 数码科技