vue+elementui Tree组件实现右键菜单

ゝ一纸荒年。 2022-12-27 14:04 324阅读 0赞

需求是tree组件实现新增与删除节点的功能,在查阅了很多文章之后,缝缝补补实现了功能

  1. npm install @xunlei/vue-context-menu --save
  2. <div id="dataPage">
  3. <el-tree
  4. id="el-tree"
  5. :data="flowTree.root"
  6. :props="flowTree.props"
  7. @node-click="selectFlow"
  8. @node-contextmenu="rightClick"/>
  9. <vue-context-menu
  10. :target="contextMenuTarget"
  11. :show="contextMenuVisible"
  12. class="right-menu"
  13. @update:show="(show) => contextMenuVisible = show">
  14. <a
  15. href="javascript:;"
  16. @click="createDatabaseOrTable">新建</a>
  17. <a
  18. href="javascript:;"
  19. @click="deleteDatabaseOrTable">删除</a>
  20. </vue-context-menu>
  21. </div>
  22. import { component as VueContextMenu } from '@xunlei/vue-context-menu';
  23. components: {
  24. 'vue-context-menu': VueContextMenu,
  25. },
  26. data(){
  27. return {
  28. flowTree: {
  29. root: [],
  30. props: {
  31. label: 'name',
  32. children: 'children',
  33. },
  34. },
  35. treeNodeData: '', // 存当前数据
  36. treeNode: '', // 存当前节点信息
  37. contextMenuVisible: false, // 让菜单显示
  38. contextMenuTarget: null,
  39. }
  40. },
  41. methods: {
  42. rightClick(e, data, node) {
  43. this.treeNodeData = data;// 存当前数据
  44. this.treeNode = node;// 存当前节点信息
  45. console.log('rightClick', this.treeNodeData, this.treeNode);
  46. this.contextMenuVisible = true;// 让菜单显示
  47. console.log('0', e, '1', e.screenX, '2', e.screenY);
  48. const ele = document.querySelector('.right-menu');
  49. ele.style.position = 'fixed';
  50. ele.style.top = `${ e.clientY}px`;
  51. ele.style.left = `${ e.clientX + 10}px`; //根据鼠标点击位置设置菜单出现
  52. },
  53. createDatabaseOrTable() {
  54. this.contextMenuVisible = false;
  55. console.log('createDatabaseOrTable', this.treeNodeData, this.treeNode);
  56. },
  57. deleteDatabaseOrTable() {
  58. this.contextMenuVisible = false;
  59. console.log('deleteDatabaseOrTable', this.treeNodeData, this.treeNode);
  60. },
  61. }
  62. <style lang="less">
  63. #dataPage {
  64. margin: 0 0;
  65. text-align: center;
  66. color: #2c3e50;
  67. height: 100%;
  68. }
  69. // 右键会选中文字,为了美观将它禁用
  70. #el-tree{
  71. user-select:none;
  72. }
  73. .right-menu {
  74. font-size: 14px;
  75. position: fixed;
  76. background: #fff;
  77. border: solid 1px rgba(0, 0, 0, .2);
  78. border-radius: 3px;
  79. z-index: 999;
  80. display: none;
  81. }
  82. .right-menu a {
  83. width: 150px;
  84. height: 28px;
  85. line-height: 28px;
  86. text-align: center;
  87. display: block;
  88. color: #1a1a1a;
  89. padding: 2px;
  90. }
  91. .right-menu a:hover {
  92. background: #bbb;
  93. color: #fff;
  94. }
  95. .right-menu {
  96. border: 1px solid #eee;
  97. box-shadow: 0 0.5em 1em 0 rgba(0,0,0,.1);
  98. border-radius: 1px;
  99. }
  100. a {
  101. text-decoration: none;
  102. }
  103. </style>

一键复制即可使用,也是在大佬们的基础上实现的,应该不会有问题,具体的功能点大家应该都能实现,有问题的话一起讨论

发表评论

表情:
评论列表 (有 0 条评论,324人围观)

还没有评论,来说两句吧...

相关阅读