el-tree右鍵菜單組件


/*********************** ContextMenu 使用說明************************************/
<jb-context-menu ref="contextMenu" class="right-menu"
:target="contextMenuTarget"
:show="contextMenuVisible"
@update:show="(show) => contextMenuVisible = show">
<a href="javascript:;" @click="handleAddNode">增加</a>
<a :class="disModify" href="javascript:;" @click="handleModifyNode">修改</a>
<a :class="disDelete" href="javascript:;" @click="handleDeleteNode">刪除</a>
</jb-context-menu>

// 當節點鼠標右鍵點擊時觸發該事件
// event--事件(判斷位置),data--該節點對應的數據屬性,
// node -- 節點對應的Node self--節點組件本身
handleRightClick (event, data, node, self) {
this.contextMenuVisible = true
this.$refs.contextMenu.setPos(event.x, event.y)
},
 
        
.right-menu
position fixed
background #fff
border solid 1px rgba(0, 0, 0, .2)
border-radius 3px
z-index 999
display none
box-shadow 0 0.5em 1em 0 rgba(0, 0, 0, .1)
a
width 75px
height 28px
line-height 28px
text-align left
display block
color #1a1a1a
text-decoration none
padding 2px 20px
&:hover
background #42b983
color #fff


/*********************** ContextMenu 組件定義************************************/
<template>
<div :style="style" style="display: block;" v-show="show"
@mousedown.stop
@contextmenu.prevent>
<slot></slot>
</div>
</template>

<script>
export default {
name: 'context-menu',
props: {
target: null,
show: Boolean
},
data () {
return {
triggerShowFn: Function,
triggerHideFn: Function,
x: null,
y: null,
style: {},
binded: false
}
},
watch: {
show (show) {
if (show) {
this.bindHideEvents()
} else {
this.unbindHideEvents()
}
},
target (target) {
this.bindEvents()
}
},
methods: {
// 初始化事件
bindEvents () {
this.$nextTick(() => {
if (!this.target || this.binded) {
return
}
this.triggerShowFn = this.contextMenuHandler.bind(this)
this.target.addEventListener('contextmenu', this.triggerShowFn)
this.binded = true
})
},

// 取消綁定事件
unbindEvents () {
if (!this.target) {
return
}
this.target.removeEventListener('contextmenu', this.triggerShowFn)
},

// 綁定隱藏菜單事件
bindHideEvents () {
this.triggerHideFn = this.clickDocumentHandler.bind(this)
document.addEventListener('mousedown', this.triggerHideFn)
document.addEventListener('mousewheel', this.triggerHideFn)
},

// 取消綁定隱藏菜單事件
unbindHideEvents () {
document.removeEventListener('mousedown', this.triggerHideFn)
document.removeEventListener('mousewheel', this.triggerHideFn)
},

// 鼠標按壓事件處理器
clickDocumentHandler (e) {
this.$emit('update:show', false)
},

// 右鍵事件事件處理
contextMenuHandler (e) {
this.x = e.clientX
this.y = e.clientY
this.layout()
this.$emit('update:show', true)
e.preventDefault()
},

// 布局
layout () {
this.style = {
left: this.x + 'px',
top: this.y + 'px'
}
},
setPos (x, y) {
this.x = x
this.y = y
this.layout()
}
},
mounted () {
this.bindEvents()
}
}
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM