element-plus表格添加简单右键

实现如下

javascript 复制代码
<template>
  <main class="mainClass"  > 

    <el-table :data="tableData" style="width: 100%"
    @row-contextmenu="rowContextmenu"
    @cell-contextmenu="cellContextmenu"
    @contextmenu.prevent
    >
    <el-table-column type="index" :index="indexMethod" />
    <el-table-column prop="date" label="Date" width="180" />
    <el-table-column prop="name" label="Name" width="180" />
    <el-table-column prop="address" label="Address" />
  </el-table>
  
  </main>

  <el-menu
    :default-active="1"
    class="el-menu-demo"
    mode="vertical"
    :collapse="isCollapse"
    v-show="menuShow"
    @close="hideMenu"
    @open="openItem"
    ref="menuRef"
    :style="{ left: left + 'px', top: top + 'px' }"
  >
    <el-menu-item index="1">Processing Center</el-menu-item>
    <el-sub-menu index="2">
      <template #title>Workspace</template>
      <el-menu-item index="2-1">item one</el-menu-item>
      <el-menu-item index="2-2">item two</el-menu-item>
      <el-menu-item index="2-3">item three</el-menu-item>
    </el-sub-menu>
  </el-menu>
  
</template>

<script setup>

import { watch ,ref } from 'vue';

const activeIndex = ref('0')
const menuShow = ref(false)
const isCollapse = ref(true)
const menuRef = ref(null)
const left = ref(0)
const top = ref(0)


const tableData = [
  {
    date: '2016-05-03',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Home',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Office',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Home',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    state: 'California',
    city: 'Los Angeles',
    address: 'No. 189, Grove St, Los Angeles',
    zip: 'CA 90036',
    tag: 'Office',
  },
]

function cellContextmenu(row,column,divstr,event)
{
  //第4个参数才是event
  console.log("cell contextenu",arguments)
  showMenu(event)
}

function rowContextmenu(row,column,event)
{
  //第3个参数才是event
  console.log("row contextenu",arguments)
}


function hideMenu()
{
  menuShow.value = false;
}
function showMenu(e)
{
  menuShow.value = true;
  left.value = e.clientX+1;
  top.value = e.clientY+1;
  //阻止默认行为  @contextmenu.prevent 同效果
  e.preventDefault(); 
}


</script>


<style scoped>
.mainClass
{
  width: 500px;
  height: 500px;
  background-color: #f0f0f0;
}
.el-menu-demo
{
  position: absolute;
  left: 0;
  top: 0;
  z-index: 100;
  width: 140px;
  /* background-color: rgb(167, 184, 184); */
}
</style>
相关推荐
神奇的程序员8 小时前
我的软件冲进苹果商店下载榜前 50 了
前端
阳光是sunny9 小时前
别再被 worktree 绕晕了!AI 编程时代你必须掌握的 Git 隔离神器
前端·人工智能·后端
万少10 小时前
万少的博客 - 技术分享与解决方案
前端·javascript·后端
尘世中一位迷途小书童12 小时前
用 Cesium 撸了一个森林火情监控大屏,弧线、粒子、发光效果都齐了
前端·javascript
IT_陈寒13 小时前
垃圾回收器选错了,我的Java服务内存炸了
前端·人工智能·后端
月光下的丝瓜13 小时前
Flutter 国内安装指南
前端·flutter
先吃饱再说14 小时前
JavaScript中`this` 的“千层套路”:从默认绑定到箭头函数的五种指向
javascript
玄星啊14 小时前
AI 编程的第 30 天,我怀念古法 Coding 了
前端·ai编程
Jolyne_14 小时前
Angular基础速通
前端·angular.js
foxire14 小时前
基于nodejs实现服务端内核引擎
javascript