Element樹形控件整合帶圖標(biāo)的下拉菜單(tree+dropdown+input)
本文主要講述:自定義樹形控件<el-tree>
需求說明:
Element UI 官網(wǎng)提供的樹形控件包含基礎(chǔ)的、可選擇的、自定義節(jié)點(diǎn)內(nèi)容的、帶節(jié)點(diǎn)過濾的以及可拖拽節(jié)點(diǎn)的樹形結(jié)構(gòu) 如下:

我想要的效果是支持搜索效果的樹,將鼠標(biāo)懸浮后顯示添加修改圖標(biāo),點(diǎn)擊圖標(biāo)后彈出對應(yīng)頁面;并且在每個文件夾前添加自定義圖標(biāo)。
實(shí)現(xiàn)效果:

實(shí)現(xiàn)步驟:
1、使用插槽(slot)
<el-col :span="4" :xs="24">
<!--目錄搜索功能-->
<div class="head-container">
<el-input
v-model="dirNameCn"
placeholder="請輸入目錄名稱"
clearable
size="small"
prefix-icon="el-icon-search"
style="margin-bottom: 20px"
/>
</div>
<!--樹的展示-->
<div class="head-container">
<el-tree
:data="dirTreeOptions"
:props="defaultProps"
:expand-on-click-node="false"
:filter-node-method="filterNode"
ref="tree"
default-expand-all
@node-click="handleNodeClick"
icon-class="el-icon-folder-opened"
node-key="id"
:check-on-click-node="true"
>
<!--隱藏的新增等圖標(biāo)-->
<span class="custom-tree-node" slot-scope="{ node, data }" @mouseenter="mouseenter(data)" @mouseleave="mouseleave(data)">
<span>{{ node.label }}</span>
<div>
<i v-show="data.show" class="el-icon-circle-plus" style="color: #00afff" @click="addDial(node, data)"/>
<!--隱藏的下拉選-->
<el-dropdown trigger="click" placement="right" @command="(command) => {handleCommand(command)}">
<i v-show="data.show" class="el-icon-more" style="color: #D3D3D3"/>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="a">重命名</el-dropdown-item>
<el-dropdown-item command="b">刪除</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</div>
</span>
</el-tree>
</div>
</el-col>
2、組件對應(yīng)的JS
注意:樹的數(shù)據(jù)是從后端查詢回來的,保存在dirTreeOptions里面
<script>
export default {
name: 'reqmdoctree',
data() {
return {
// 左側(cè)搜索框內(nèi)容
dirNameCn: '',
// 目錄樹選項
dirTreeOptions: undefined,
defaultProps: {
children: "children",
label: "label"
},
// 樹形菜單中有無子節(jié)點(diǎn)
yesChild: undefined,
// 控制左側(cè)新增提示信息框
show: 0,
// 查詢需求文檔信息參數(shù)
queryParams: {
docNo: undefined, // 文檔編號
docNameEn: undefined, // 文檔英文名稱
dirNo: undefined,// 目錄編號
current: 1, // 當(dāng)前頁數(shù)
size: 20 // 每頁顯示多少條
},
treeId: undefined,
}
},
methods: {
/** 查詢需求目錄下拉樹結(jié)構(gòu) */
getTreeselect() {
treeselect().then(response => {
this.dirTreeOptions = response.data
})
},
// 搜索值為過濾函數(shù)
filterNode(value, data) {
if (!value) return true
return data.label.indexOf(value) !== -1
},
// 節(jié)點(diǎn)被點(diǎn)擊時的回調(diào)函數(shù)
handleNodeClick(data) {
// console.log(data)
this.treeId = data.id
this.yesChild = data.children
this.queryParams.dirNo = data.id
this.getList()
},
// 樹中三個點(diǎn)的事件
handleCommand(command) {
if (command == 'a') {
selectReqNo(this.treeId).then(response => {
this.uuid = response.msg
getObjTree(response.msg).then(response => {
this.form = response.data
this.open = true
this.title = '修改需求文檔目錄配置表'
})
})
}
if (command == 'b') {
if (this.yesChild != undefined) {
this.$notify.error({
title: '警告',
message: '此目錄下還有別的文件夾'
})
} else {
selectReqNo(this.treeId).then(response => {
this.uuid = response.msg
this.$confirm('是否確認(rèn)刪除ID為' + this.uuid + '的數(shù)據(jù)項?', '警告', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning'
}).then(()=>{
return delObjTree(this.uuid)
}).then(data => {
this.getTreeselect()
this.msgSuccess('刪除成功')
}).catch(function() {
})
})
}
}
},
// 左側(cè)新建目錄/文件
addDial(node, data) {
// console.log(node, '---', data)
this.reset()
this.form.dirParentId = data.id
this.open = true
this.title = '添加需求文檔目錄配置表'
},
// 左側(cè)鼠標(biāo)懸浮展示圖標(biāo)
mouseenter(data){
this.$set(data, 'show', true)
},
// 左側(cè)鼠標(biāo)離開不展示圖標(biāo)
mouseleave(data){
this.$set(data, 'show', false)
},
//打開新增資源彈窗 這里略......
}
}
</script>
說明:
參考文檔:element UI、樹形控件整合下拉選
到此這篇關(guān)于Element樹形控件整合帶圖標(biāo)的下拉菜單(tree+dropdown+input)的文章就介紹到這了,更多相關(guān)Element帶圖標(biāo)的下拉菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue插件實(shí)現(xiàn)過程中遇到的問題總結(jié)
隨著Vue.js越來越火,Vue.js 的相關(guān)插件也在不斷的被貢獻(xiàn)出來,數(shù)不勝數(shù),這篇文章主要給大家介紹了關(guān)于Vue插件實(shí)現(xiàn)過程中遇到的問題,需要的朋友可以參考下2021-08-08
Vue實(shí)現(xiàn)無限輪播效果時動態(tài)綁定style失效的解決方法
最近在開發(fā)中遇到了一個新需求:列表輪播滾動,實(shí)現(xiàn)方式也有很多,比如使用第三方插件,但是由于不想依賴第三方插件,想自己實(shí)現(xiàn),于是我開始了嘗試,但是在這個過程中遇到了動態(tài)綁定style樣式不生效,所以本文介紹了Vue實(shí)現(xiàn)無限輪播效果時動態(tài)綁定style失效的解決方法2024-08-08
淺談vue后臺管理系統(tǒng)權(quán)限控制思考與實(shí)踐
這篇文章主要介紹了淺談vue后臺管理系統(tǒng)權(quán)限控制思考與實(shí)踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Vue中Number方法將字符串轉(zhuǎn)換為數(shù)字的過程
這篇文章主要介紹了Vue中Number方法將字符串轉(zhuǎn)換為數(shù)字,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
如何使用vue3簡單實(shí)現(xiàn)WebSocket通信
這篇文章主要給大家介紹了關(guān)于如何使用vue3簡單實(shí)現(xiàn)WebSocket通信的相關(guān)資料,WebSocket是全雙工網(wǎng)絡(luò)通信通信協(xié)議,實(shí)現(xiàn)了客戶端和服務(wù)器的平等對話,任何一方都可以主動發(fā)送數(shù)據(jù),需要的朋友可以參考下2023-08-08
Vue中keyup.enter和blur事件沖突的問題及解決
這篇文章主要介紹了Vue中keyup.enter和blur事件沖突的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

