el-tree右键菜单及优化
程序员文章站
2022-05-10 19:05:38
...
el-tree右键菜单及优化
<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 id="menu" v-show="menuVisible" @mouseleave="menuVisible=!menuVisible">
<el-card class="box-card">
<div class="text item">
<el-link icon="el-icon-circle-plus-outline" :underline="false">添加</el-link>
</div>
<div class="text item">
<el-link icon="el-icon-remove-outline" :underline="false">删除</el-link> </div>
</el-card>
</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>
.text {
font-size: 14px;
}
.item {
margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both
}
.box-card {
width: 100px;
}
</style>