<template>
<div>
<el-table :data="manageData.tableData" height="100%" size="mini"
@row-contextmenu="handleRow">
<el-table-column prop="sensorCode" header-align="center" align="center"
label="探测器编号"></el-table-column>
</el-table>
<!-- 自定义 tooltip 弹出框 -->
<div :style="tooltipStyle" v-if="tooltipVisible" class="custom-tooltip"
@click="ignoreAlarm"> 忽略报警
</div>
</div>
</template>
<script>
export default {
data(){
return{
currentRowData: null, // 保存当前行的数据
tooltipVisible: false,
tooltipStyle: {
position: 'absolute',
left: '0px',
top: '0px',
zIndex: 9999,
},
}
},
mounted() {
document.addEventListener('click', this.handleDocumentClick); // 添加全局点击事件监听器
},
beforeDestroy() {
document.removeEventListener('click', this.handleDocumentClick);
},
methods: {
// 点击鼠标左键空白区域就关闭提示
handleDocumentClick(event) {
if (this.tooltipVisible && (!this.$refs.tooltip ||
!this.$refs.tooltip.contains(event.target))) {
this.tooltipVisible = false; // 如果点击的不是 tooltip 或其子元素,关闭 tooltip
}
},
// 自定义鼠标右键事件
handleRow(row, event) {
this.currentRowData = row
event.preventDefault();
const tooltipWidth = 70; // 定义 Tooltip 宽度
const tooltipHeight = 20; // 定义 Tooltip 高度
let x = event.clientX;
let y = event.clientY;
// 确保 Tooltip 不超出屏幕右边界
if (x + tooltipWidth > window.innerWidth) {
x -= tooltipWidth;
}
// 确保 Tooltip 不超出屏幕底部边界
if (y + tooltipHeight > window.innerHeight) {
y -= tooltipHeight;
}
this.tooltipStyle = {
position: 'fixed', // 使用 fixed 以便相对于视窗定位
left: `${x}px`,
top: `${y}px`,
zIndex: 9999,
width: `${tooltipWidth}px`,
height: `${tooltipHeight}px`,
backgroundColor: '#152841',
border: '1px solid #ccc',
padding: '5px',
borderRadius: '4px',
};
this.tooltipVisible = true;
},
}
}
vue2.0、elment-ui-table 行,自定义鼠标右键弹出内容
最新推荐文章于 2025-05-20 23:46:10 发布