ToB企服应用市场:ToB评测及商务社交产业平台

标题: element-plus表格添加简单右键 [打印本页]

作者: 用多少眼泪才能让你相信    时间: 2024-6-11 12:45
标题: element-plus表格添加简单右键

实现如下
  1. <template>
  2.   <main class="mainClass"  >
  3.     <el-table :data="tableData" style="width: 100%"
  4.     @row-contextmenu="rowContextmenu"
  5.     @cell-contextmenu="cellContextmenu"
  6.     @contextmenu.prevent
  7.     >
  8.     <el-table-column type="index" :index="indexMethod" />
  9.     <el-table-column prop="date" label="Date" width="180" />
  10.     <el-table-column prop="name" label="Name" width="180" />
  11.     <el-table-column prop="address" label="Address" />
  12.   </el-table>
  13.   
  14.   </main>
  15.   <el-menu
  16.     :default-active="1"
  17.     class="el-menu-demo"
  18.     mode="vertical"
  19.     :collapse="isCollapse"
  20.     v-show="menuShow"
  21.     @close="hideMenu"
  22.     @open="openItem"
  23.     ref="menuRef"
  24.     :style="{ left: left + 'px', top: top + 'px' }"
  25.   >
  26.     <el-menu-item index="1">Processing Center</el-menu-item>
  27.     <el-sub-menu index="2">
  28.       <template #title>Workspace</template>
  29.       <el-menu-item index="2-1">item one</el-menu-item>
  30.       <el-menu-item index="2-2">item two</el-menu-item>
  31.       <el-menu-item index="2-3">item three</el-menu-item>
  32.     </el-sub-menu>
  33.   </el-menu>
  34.   
  35. </template>
  36. <script setup>
  37. import { watch ,ref } from 'vue';
  38. const activeIndex = ref('0')
  39. const menuShow = ref(false)
  40. const isCollapse = ref(true)
  41. const menuRef = ref(null)
  42. const left = ref(0)
  43. const top = ref(0)
  44. const tableData = [
  45.   {
  46.     date: '2016-05-03',
  47.     name: 'Tom',
  48.     state: 'California',
  49.     city: 'Los Angeles',
  50.     address: 'No. 189, Grove St, Los Angeles',
  51.     zip: 'CA 90036',
  52.     tag: 'Home',
  53.   },
  54.   {
  55.     date: '2016-05-02',
  56.     name: 'Tom',
  57.     state: 'California',
  58.     city: 'Los Angeles',
  59.     address: 'No. 189, Grove St, Los Angeles',
  60.     zip: 'CA 90036',
  61.     tag: 'Office',
  62.   },
  63.   {
  64.     date: '2016-05-04',
  65.     name: 'Tom',
  66.     state: 'California',
  67.     city: 'Los Angeles',
  68.     address: 'No. 189, Grove St, Los Angeles',
  69.     zip: 'CA 90036',
  70.     tag: 'Home',
  71.   },
  72.   {
  73.     date: '2016-05-01',
  74.     name: 'Tom',
  75.     state: 'California',
  76.     city: 'Los Angeles',
  77.     address: 'No. 189, Grove St, Los Angeles',
  78.     zip: 'CA 90036',
  79.     tag: 'Office',
  80.   },
  81. ]
  82. function cellContextmenu(row,column,divstr,event)
  83. {
  84.   //第4个参数才是event
  85.   console.log("cell contextenu",arguments)
  86.   showMenu(event)
  87. }
  88. function rowContextmenu(row,column,event)
  89. {
  90.   //第3个参数才是event
  91.   console.log("row contextenu",arguments)
  92. }
  93. function hideMenu()
  94. {
  95.   menuShow.value = false;
  96. }
  97. function showMenu(e)
  98. {
  99.   menuShow.value = true;
  100.   left.value = e.clientX+1;
  101.   top.value = e.clientY+1;
  102.   //阻止默认行为  @contextmenu.prevent 同效果
  103.   e.preventDefault();
  104. }
  105. </script>
  106. <style scoped>
  107. .mainClass
  108. {
  109.   width: 500px;
  110.   height: 500px;
  111.   background-color: #f0f0f0;
  112. }
  113. .el-menu-demo
  114. {
  115.   position: absolute;
  116.   left: 0;
  117.   top: 0;
  118.   z-index: 100;
  119.   width: 140px;
  120.   /* background-color: rgb(167, 184, 184); */
  121. }
  122. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4