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>
相关推荐
m0_7482552638 分钟前
前端安全——敏感信息泄露
前端·安全
鑫~阳2 小时前
html + css 淘宝网实战
前端·css·html
Catherinemin2 小时前
CSS|14 z-index
前端·css
漫天转悠2 小时前
Vue3项目中引入TailwindCSS(图文详情)
vue.js
qq_589568103 小时前
Echarts+vue电商平台数据可视化——后台实现笔记
vue.js·信息可视化·echarts
2401_882727574 小时前
低代码配置式组态软件-BY组态
前端·后端·物联网·低代码·前端框架
NoneCoder4 小时前
CSS系列(36)-- Containment详解
前端·css
anyup_前端梦工厂4 小时前
初始 ShellJS:一个 Node.js 命令行工具集合
前端·javascript·node.js
5hand4 小时前
Element-ui的使用教程 基于HBuilder X
前端·javascript·vue.js·elementui
GDAL4 小时前
vue3入门教程:ref能否完全替代reactive?
前端·javascript·vue.js