index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <!-- 右键 -->
  3. <div class="context-menu-container"
  4. @click="updateContextMenu(false)"
  5. @contextmenu.prevent="onContextMenu">
  6. <slot></slot>
  7. <contextMenu :contextMenu="menuContext"
  8. @update="updateContextMenu" />
  9. </div>
  10. </template>
  11. <script lang="ts">
  12. import { defineComponent, PropType, reactive } from 'vue';
  13. import contextMenu from './contextMenu.vue';
  14. import { ContextMenu, MenuItem } from './interface';
  15. export default defineComponent({
  16. name: 'context-menu-container',
  17. props: {
  18. contextMenuList: {
  19. default: [],
  20. type: Array as PropType<MenuItem[]>,
  21. },
  22. tableList: {
  23. default: [],
  24. type: Array as PropType<any[]>,
  25. },
  26. selectedRow: {
  27. default: null,
  28. type: Object,
  29. },
  30. },
  31. components: {
  32. contextMenu,
  33. },
  34. setup(props) {
  35. const menuContext = reactive<ContextMenu>({
  36. position: { clientX: 0, clientY: 0 },
  37. show: false,
  38. menuList: props.contextMenuList,
  39. selectedData: props.selectedRow,
  40. });
  41. function onContextMenu(value: MouseEvent) {
  42. const target = value.target as any;
  43. // 获取点击表格的 tr 所在的 索引位置
  44. const index = target.parentElement.rowIndex;
  45. // 过滤右键表头
  46. if (index) {
  47. const { clientX, clientY } = value;
  48. Object.assign(menuContext.position, { clientX, clientY });
  49. menuContext.show = true;
  50. // menuContext.selectedData = props.tableList[index - 1];
  51. }
  52. }
  53. // 关闭右键弹窗
  54. function updateContextMenu(value: boolean) {
  55. menuContext.show = value;
  56. }
  57. return { menuContext, onContextMenu, updateContextMenu };
  58. },
  59. });
  60. </script>
  61. <style lang="less">
  62. .context-menu {
  63. min-width: 100px;
  64. user-select: none;
  65. position: fixed;
  66. background-color: #282e34;
  67. z-index: 999;
  68. border-radius: 2px;
  69. border: 1px solid #2e3539;
  70. padding-left: 0;
  71. list-style: none;
  72. margin-bottom: 0;
  73. cursor: pointer;
  74. li {
  75. padding: 6px;
  76. color: #88a0ae;
  77. font-size: 16px;
  78. border-bottom: 1px solid #2e3539;
  79. text-align: center;
  80. &:hover {
  81. color: #e5e5e5;
  82. background-color: #343e48;
  83. }
  84. }
  85. }
  86. </style>;