vue elementui tree 任意級(jí)別拖拽功能代碼
我的是根據(jù)父級(jí)id做的一些判斷
<el-tree draggable :allow-drop='allowDrop' @node-drop='sort' accordion 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='暫無(wú)數(shù)據(jù)'> allowDrop(draggingNode, dropNode, type){ //注掉的是同級(jí)拖拽 /* if (draggingNode.data.level === dropNode.data.level) { if (draggingNode.data.aboveId === dropNode.data.aboveId) { return type === ’prev’ || type === ’next’ } } else { // 不同級(jí)進(jìn)行處理 return false } */ //任意級(jí)別拖拽 if (draggingNode.data.aboveId === dropNode.data.aboveId) { return type === ’prev’ || type === ’next’ } else { return type === ’prev’ || type === ’next’ || type === ’inner’ } }, //拖拽完成之后要重新排序 /* * draggingNode:被拖拽節(jié)點(diǎn)對(duì)應(yīng)的 Node * dropNode:結(jié)束拖拽時(shí)最后進(jìn)入的節(jié)點(diǎn) * type: 被拖拽節(jié)點(diǎn)的放置位置(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) } }) }
補(bǔ)充知識(shí):element-ui tree 實(shí)現(xiàn)同級(jí)拖拽
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
<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'>通過(guò) node 獲取</el-button> <el-button @click='getCheckedKeys'>通過(guò) key 獲取</el-button> <el-button @click='setCheckedNodes'>通過(guò) node 設(shè)置</el-button> <el-button @click='setCheckedKeys'>通過(guò) key 設(shè)置</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: '二級(jí) 2-1' }, { id: 9, label: '三級(jí) 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: '一級(jí) 1', children: [ { id: 4, label: '二級(jí) 1-1', prop: '4' } ] }, { id: 2, label: '一級(jí) 2', children: [ { id: 5, label: '二級(jí) 2-1', prop: '5' }, { id: 6, label: '二級(jí) 2-2', prop: '6' } ] }, { id: 3, label: '一級(jí) 3', children: [ { id: 7, label: '二級(jí) 3-1', prop: '7' }, { id: 8, label: '二級(jí) 3-2', prop: '9' } ] }, { id: 9, label: '一級(jí)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 { // 不同級(jí)進(jìn)行處理 return false; } }, sort(draggingNode, dropNode, type, event) { // console.log(’排序’) // console.log('<><><>>><><<><><><><><><><>') // 拖拽之后的重新組合的數(shù)組 // 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 任意級(jí)別拖拽功能代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息2. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明4. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問(wèn)題8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. CSS hack用法案例詳解10. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)
