el-tree鼠标右键自定义菜单
程序员文章站
2022-05-10 19:06:02
...
代码
<template>
<div id="app">
<div class="block">
<el-tree :data="data" node-key="id" default-expand-all :expand-on-click-node="false"
@node-contextmenu="rightClick"
>
<span class="custom-tree-node" slot-scope="{ node, data }">
<span> {{ node.label }}</span>
</span>
</el-tree>
</div>
<div v-show="menuVisible" @mouseleave="menuVisible=!menuVisible">
<ul id="menu">
<li tabindex="-1" class="menu__item" id="menu-4994-1-0" @click="() => treeAdd(nodeData)">
<el-link icon="el-icon-circle-plus-outline" :underline="false">添加</el-link>
</li>
<li tabindex="-1" class="menu__item" id="menu-4994-1-2" @click="() =>treeRemove(nodeData)">
<el-link icon="el-icon-remove-outline" :underline="false">删除</el-link>
</li>
</ul>
</div>
</div>
</template>
<script>
export default {
data() {
return {
nodeData: {},
menuVisible: false,
data: [{
id: 1,
icon: "el-icon-delete",
label: '一级 1',
children: [{
id: 4,
label: '二级 1-1',
children: [{
id: 9,
icon: 'el-icon-info',
label: '三级 1-1-1'
}, {
id: 10,
label: '三级 1-1-2'
}]
}]
}, {
id: 2,
label: '一级 2',
children: [{
id: 5,
label: '二级 2-1'
}, {
id: 6,
label: '二级 2-2'
}]
}, {
id: 3,
label: '一级 3',
children: [{
id: 7,
label: '二级 3-1'
}, {
id: 8,
label: '二级 3-2'
}]
}]
}
},
methods: {
rightClick(MouseEvent, object, Node, element) {
this.nodeData = Node
debugger
this.menuVisible = true;
let menu = document.querySelector("#menu");
menu.style.cssText = "position: fixed; left: " + (MouseEvent.clientX - 10) + 'px' + "; top: " + (MouseEvent.clientY - 25) + 'px; z-index: 999; cursor:pointer;';
},
treeAdd() {
alert(this.nodeData)
},
treeRemove() {
this.menuVisible = true
}
}
}
</script>
<style>
.menu__item {
width: 100%;
display: block;
line-height: 20px;
text-align: center;
margin-top: 10px;
text-align: center;
}
</style>