這篇文章主要介紹怎么基于Vue實(shí)現(xiàn)可以拖拽的樹(shù)形表格,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司是一家企業(yè)級(jí)云計(jì)算解決方案提供商,超15年IDC數(shù)據(jù)中心運(yùn)營(yíng)經(jīng)驗(yàn)。主營(yíng)GPU顯卡服務(wù)器,站群服務(wù)器,達(dá)州服務(wù)器托管,海外高防服務(wù)器,成都機(jī)柜租用,動(dòng)態(tài)撥號(hào)VPS,海外云手機(jī),海外云服務(wù)器,海外服務(wù)器租用托管等。Vue具體輕量級(jí)框架、簡(jiǎn)單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢(shì),Vue中頁(yè)面使用的是局部刷新,不用每次跳轉(zhuǎn)頁(yè)面都要請(qǐng)求所有數(shù)據(jù)和dom,可以大大提升訪(fǎng)問(wèn)速度和用戶(hù)體驗(yàn)。
因業(yè)務(wù)需求,需要一個(gè)樹(shù)形表格,并且支持拖拽排序,任意未知插入,github搜了下,真不到合適的,大部分樹(shù)形表格都沒(méi)有拖拽功能,所以決定自己實(shí)現(xiàn)一個(gè)。這里分享一下實(shí)現(xiàn)過(guò)程,項(xiàng)目源代碼請(qǐng)看github,插件已打包封裝好,發(fā)布到npm上
本博文會(huì)分為兩部分,第一部分為使用方式,第二部分為實(shí)現(xiàn)方式
安裝方式
npm i drag-tree-table --save-dev
使用方式
import dragTreeTable from 'drag-tree-table'
模版寫(xiě)法
<dragTreeTable :data="treeData" :onDrag="onTreeDataChange"></dragTreeTable>
data參數(shù)示例
{ lists: [ { "id":40, "parent_id":0, "order":0, "name":"動(dòng)物類(lèi)", "open":true, "lists":[] },{ "id":5, "parent_id":0, "order":1, "name":"昆蟲(chóng)類(lèi)", "open":true, "lists":[ { "id":12, "parent_id":5, "open":true, "order":0, "name":"螞蟻", "lists":[] } ] }, { "id":19, "parent_id":0, "order":2, "name":"植物類(lèi)", "open":true, "lists":[] } ], columns: [ { type: 'selection', title: '名稱(chēng)', field: 'name', width: 200, align: 'center', formatter: (item) => { return '<a>'+item.name+'</a>' } }, { title: '操作', type: 'action', width: 350, align: 'center', actions: [ { text: '查看角色', onclick: this.onDetail, formatter: (item) => { return '<i>查看角色</i>' } }, { text: '編輯', onclick: this.onEdit, formatter: (item) => { return '<i>編輯</i>' } } ] }, ] }
onDrag在表格拖拽時(shí)觸發(fā),返回新的list
onTreeDataChange(lists) { this.treeData.lists = lists }
到這里組件的使用方式已經(jīng)介紹完畢
實(shí)現(xiàn)
?遞歸生成樹(shù)姓結(jié)構(gòu)(非JSX方式實(shí)現(xiàn))
?實(shí)現(xiàn)拖拽排序(借助H5的dragable屬性)
?單元格內(nèi)容自定義展示
組件拆分-共分為四個(gè)組件
dragTreeTable.vue是入口組件,定義整體結(jié)構(gòu)
row是遞歸組件(核心組件)
clolmn單元格,內(nèi)容承載
space控制縮進(jìn)
看一下dragTreeTable的結(jié)構(gòu)
<template> <div class="drag-tree-table"> <div class="drag-tree-table-header"> <column v-for="(item, index) in data.columns" :width="item.width" :key="index" > {{item.title}} </column> </div> <div class="drag-tree-table-body" @dragover="draging" @dragend="drop"> <row depth="0" :columns="data.columns" :model="item" v-for="(item, index) in data.lists" :key="index"> </row> </div> </div> </template>
看起來(lái)分原生table很像,dragTreeTable主要定義了tree的框架,并實(shí)現(xiàn)拖拽邏輯
filter函數(shù)用來(lái)匹配當(dāng)前鼠標(biāo)懸浮在哪個(gè)行內(nèi),并分為三部分,上中下,并對(duì)當(dāng)前匹配的行進(jìn)行高亮
resetTreeData當(dāng)drop觸發(fā)時(shí)調(diào)用,該方法會(huì)重新生成一個(gè)新的排完序的數(shù)據(jù),然后返回父組件
下面是所有實(shí)現(xiàn)代碼
<script> import row from './row.vue' import column from './column.vue' import space from './space.vue' document.body.ondrop = function (event) { event.preventDefault(); event.stopPropagation(); } export default { name: "dragTreeTable", components: { row, column, space }, props: { data: Object, onDrag: Function }, data() { return { treeData: [], dragX: 0, dragY: 0, dragId: '', targetId: '', whereInsert: '' } }, methods: { getElementLeft(element) { var actualLeft = element.offsetLeft; var current = element.offsetParent; while (current !== null){ actualLeft += current.offsetLeft; current = current.offsetParent; } return actualLeft }, getElementTop(element) { var actualTop = element.offsetTop; var current = element.offsetParent; while (current !== null) { actualTop += current.offsetTop; current = current.offsetParent; } return actualTop }, draging(e) { if (e.pageX == this.dragX && e.pageY == this.dragY) return this.dragX = e.pageX this.dragY = e.pageY this.filter(e.pageX, e.pageY) }, drop(event) { this.clearHoverStatus() this.resetTreeData() }, filter(x,y) { var rows = document.querySelectorAll('.tree-row') this.targetId = undefined for(let i=0; i < rows.length; i++) { const row = rows[i] const rx = this.getElementLeft(row); const ry = this.getElementTop(row); const rw = row.clientWidth; const rh = row.clientHeight; if (x > rx && x < (rx + rw) && y > ry && y < (ry + rh)) { const diffY = y - ry const hoverBlock = row.children[row.children.length - 1] hoverBlock.style.display = 'block' const targetId = row.getAttribute('tree-id') if (targetId == window.dragId){ this.targetId = undefined return } this.targetId = targetId let whereInsert = '' var rowHeight = document.getElementsByClassName('tree-row')[0].clientHeight if (diffY/rowHeight > 3/4) { console.log(111, hoverBlock.children[2].style) if (hoverBlock.children[2].style.opacity !== '0.5') { this.clearHoverStatus() hoverBlock.children[2].style.opacity = 0.5 } whereInsert = 'bottom' } else if (diffY/rowHeight > 1/4) { if (hoverBlock.children[1].style.opacity !== '0.5') { this.clearHoverStatus() hoverBlock.children[1].style.opacity = 0.5 } whereInsert = 'center' } else { if (hoverBlock.children[0].style.opacity !== '0.5') { this.clearHoverStatus() hoverBlock.children[0].style.opacity = 0.5 } whereInsert = 'top' } this.whereInsert = whereInsert } } }, clearHoverStatus() { var rows = document.querySelectorAll('.tree-row') for(let i=0; i < rows.length; i++) { const row = rows[i] const hoverBlock = row.children[row.children.length - 1] hoverBlock.style.display = 'none' hoverBlock.children[0].style.opacity = 0.1 hoverBlock.children[1].style.opacity = 0.1 hoverBlock.children[2].style.opacity = 0.1 } }, resetTreeData() { if (this.targetId === undefined) return const newList = [] const curList = this.data.lists const _this = this function pushData(curList, needPushList) { for( let i = 0; i < curList.length; i++) { const item = curList[i] var obj = _this.deepClone(item) obj.lists = [] if (_this.targetId == item.id) { const curDragItem = _this.getCurDragItem(_this.data.lists, window.dragId) if (_this.whereInsert === 'top') { curDragItem.parent_id = item.parent_id needPushList.push(curDragItem) needPushList.push(obj) } else if (_this.whereInsert === 'center'){ curDragItem.parent_id = item.id obj.lists.push(curDragItem) needPushList.push(obj) } else { curDragItem.parent_id = item.parent_id needPushList.push(obj) needPushList.push(curDragItem) } } else { if (window.dragId != item.id) needPushList.push(obj) } if (item.lists && item.lists.length) { pushData(item.lists, obj.lists) } } } pushData(curList, newList) this.onDrag(newList) }, deepClone (aObject) { if (!aObject) { return aObject; } var bObject, v, k; bObject = Array.isArray(aObject) ? [] : {}; for (k in aObject) { v = aObject[k]; bObject[k] = (typeof v === "object") ? this.deepClone(v) : v; } return bObject; }, getCurDragItem(lists, id) { var curItem = null var _this = this function getchild(curList) { for( let i = 0; i < curList.length; i++) { var item = curList[i] if (item.id == id) { curItem = JSON.parse(JSON.stringify(item)) break } else if (item.lists && item.lists.length) { getchild(item.lists) } } } getchild(lists) return curItem; } } } </script>
row組件核心在于遞歸,并注冊(cè)拖拽事件,v-html支持傳入函數(shù),這樣可以實(shí)現(xiàn)自定義展示,渲染數(shù)據(jù)時(shí)需要判斷是否有子節(jié)點(diǎn),有的畫(huà)遞歸調(diào)用本身,并傳入子節(jié)點(diǎn)數(shù)據(jù)
結(jié)構(gòu)如下
<template> <div class="tree-block" draggable="true" @dragstart="dragstart($event)" @dragend="dragend($event)"> <div class="tree-row" @click="toggle" :tree-id="model.id" :tree-p-id="model.parent_id"> <column v-for="(subItem, subIndex) in columns" v-bind:class="'align-' + subItem.align" :field="subItem.field" :width="subItem.width" :key="subIndex"> <span v-if="subItem.type === 'selection'"> <space :depth="depth"/> <span v-if = "model.lists && model.lists.length" class="zip-icon" v-bind:class="[model.open ? 'arrow-bottom' : 'arrow-right']"> </span> <span v-else class="zip-icon arrow-transparent"> </span> <span v-if="subItem.formatter" v-html="subItem.formatter(model)"></span> <span v-else v-html="model[subItem.field]"></span> </span> <span v-else-if="subItem.type === 'action'"> <a class="action-item" v-for="(acItem, acIndex) in subItem.actions" :key="acIndex" type="text" size="small" @click.stop.prevent="acItem.onclick(model)"> <i :class="acItem.icon" v-html="acItem.formatter(model)"></i> </a> </span> <span v-else-if="subItem.type === 'icon'"> {{model[subItem.field]}} </span> <span v-else> {{model[subItem.field]}} </span> </column> <div class="hover-model" > <div class="hover-block prev-block"> <i class="el-icon-caret-top"></i> </div> <div class="hover-block center-block"> <i class="el-icon-caret-right"></i> </div> <div class="hover-block next-block"> <i class="el-icon-caret-bottom"></i> </div> </div> </div> <row v-show="model.open" v-for="(item, index) in model.lists" :model="item" :columns="columns" :key="index" :depth="depth * 1 + 1" v-if="isFolder"> </row> </div> </template> <script> import column from './column.vue' import space from './space.vue' export default { name: 'row', props: ['model','depth','columns'], data() { return { open: false, visibility: 'visible' } }, components: { column, space }, computed: { isFolder() { return this.model.lists && this.model.lists.length } }, methods: { toggle() { if(this.isFolder) { this.model.open = !this.model.open } }, dragstart(e) { e.dataTransfer.setData('Text', this.id); window.dragId = e.target.children[0].getAttribute('tree-id') e.target.style.opacity = 0.2 }, dragend(e) { e.target.style.opacity = 1; } } }
clolmn和space比較簡(jiǎn)單,這里就不過(guò)多闡述
上面就是整個(gè)實(shí)現(xiàn)過(guò)程,組件在chrome上運(yùn)行穩(wěn)定,因?yàn)橛肏5的dragable,所以兼容會(huì)有點(diǎn)問(wèn)題,后續(xù)會(huì)修改拖拽的實(shí)現(xiàn)方式,手動(dòng)實(shí)現(xiàn)拖拽
以上是“怎么基于Vue實(shí)現(xiàn)可以拖拽的樹(shù)形表格”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站標(biāo)題:怎么基于Vue實(shí)現(xiàn)可以拖拽的樹(shù)形表格-創(chuàng)新互聯(lián)
新聞來(lái)源:http://aaarwkj.com/article10/dpihgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站改版、企業(yè)網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)公司、用戶(hù)體驗(yàn)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容