貼入樹的load代碼,添加ul是為了鼠標右擊展示操作選項的
<div class="block device-tree"> <div v-show="showOpertions"> <ul id="menu" class="right-menu" > <li class="el-icon-plus menu-item" @click="() => add(this.nodes, this.datas)" > 新增 </li> <li class="el-icon-edit menu-item" @click="() => edit(this.nodes, this.datas)" > 編輯 </li> <li class="el-icon-minus menu-item" @click="() => remove(this.nodes, this.datas)" > 刪除 </li> </ul> </div> <el-input v-model="filterText" class="hbaseInputClass" placeholder="輸入關鍵字進行過濾" > </el-input> <el-tree v-if="openPanel" ref="tree" class="filter-tree" :props="defaultProps" node-key="path" :default-expanded-keys="treeExpandData" :load="loadData" lazy :data="data" :filter-node-method="filterNode" @node-contextmenu="rightClick" @node-click="handleNodeClick" > <span slot-scope="{ node, data}" class="custom-tree-node"> <!-- 不同節點類型展示logo不同--> <span v-if="data.flag === 'namespace'" class="el-icon-folder-opened">{{ node.label }}</span> <span v-if="data.flag === 'table'" class="el-icon-folder-opened">{{ node.label }}</span> <span v-if="data.flag === 'family'" class="el-icon-folder-opened">{{ node.label }}</span> </span> </el-tree> </div>
// 獲取樹形結構默認展開節點,獲取到樹的數據后,就調這個接口,傳入node-key綁定的需要展開的節點的path值 getRoleTreeRootNode(provincialCenterPath) { this.treeExpandData.push(provincialCenterPath) },
rightClick(event, data, node, obj) { this.showOpertions = false // 先把模態框關死,目的是:第二次或者第n次右鍵鼠標的時候 它默認的是true this.showOpertions = true const menu = document.querySelector('#menu') menu.style.left = event.clientX - 220 + 'px' menu.style.top = event.clientY - 85 + 'px' // 給整個document添加監聽鼠標事件,點擊任何位置執行closeRightMenu方法,及時將菜單關閉 document.addEventListener('click', this.closeRightMenu) }, closeRightMenu() { this.showOpertions = false // 及時關掉鼠標監聽事件 document.removeEventListener('click', this.closeRightMenu) },
loadData(node, resolve) { this.$nextTick(() => { if (node.level === 0) { resolve(this.data) } if (node.level >= 1) { this.getIndex(node, resolve) } }) }, getIndex(node, resolve) { this.$get('***/**/***/**', { 'path': this.folderPath }).then(data => { const array = [] for (let i = 0; i < data.length; i++) { const jsonList = {} jsonList.name = data[i].name jsonList.path = data[i].path if (data[i].fileFlag === false) { jsonList.leaf = false } else { jsonList.leaf = true } array.push(jsonList) } return resolve(array) }).catch(data => { }) },
鼠標右鍵彈出操作欄的樣式設置:
1 .right-menu { 2 z-index: 1; 3 position: absolute; 4 height: 100px; 5 width: 100px; 6 position: absolute; 7 border-radius: 5px; 8 border: 1px solid #ccc; 9 background-color: white; 10 } 11 .menu-item { 12 display: block; 13 line-height: 20px; 14 text-align: left; 15 margin-top: 10px; 16 font-size: 14px; 17 color: #606266; 18 } 19 li:hover { 20 background-color: #edf6ff; 21 color: #606266; 22 }