vue3 element plus el-tree 添加右键菜单

工作上需要给 element plus 的 tree 组件添加右键菜单,参考网上的示例简单实现了一个,开箱即用,简单方便。代码和使用示例如下:

ContextMenu.vue

<template>
  <div
    v-show="visible"
    :style="{
      left: position.left + 'px',
      top: position.top + 'px',
      display: visible ? 'block' : 'none',
    }"
    class="context-menu"
  >
    <div
      v-for="(item, i) in menuItems"
      :key="i"
      class="menu-item"
      @click="item.action(rightClickItem)"
    >
      {{ item.name }}
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref } from "vue";

interface Props {
  menuItems: ContextMenuItem[];
}

export interface ContextMenuItem {
  name: string;
  icon?: string;
  action: (rightClickItem: any) => void;
}

const props = defineProps<Props>();
const visible = ref(false);
const rightClickItem = ref(null);
const position = ref({
  top: 0,
  left: 0,
});

const openMenu = (e: MouseEvent, item: any) => {
  let menuCount = props.menuItems.length;
  let windowHeight = window.innerHeight;

  visible.value = true;
  position.value.top = Math.min(e.pageY, windowHeight - 40 - menuCount * 32);
  position.value.left = e.pageX;
  rightClickItem.value = item;
};

const closeMenu = () => {
  visible.value = false;
};

watch(visible, () => {
  if (visible.value) {
    document.body.addEventListener("click", closeMenu);
  } else {
    document.body.removeEventListener("click", closeMenu);
  }
});

defineExpose({ openMenu, closeMenu });
</script>
<style scoped lang="less">
.context-menu {
  margin: 0;
  background: #fff;
  z-index: 2;
  position: absolute;
  list-style-type: none;
  padding: 4px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 400;
  color: #333;
  box-shadow: 2px 2px 2px 2px rgba(0, 0, 0, 0.3);

  .menu-item {
    padding: 0 15px;
    height: 32px;
    line-height: 32px;
    color: rgb(29, 33, 41);
    cursor: pointer;
  }
  .menu-item:hover {
    background: var(--el-color-primary-light-9);
    border-radius: 4px;
  }
}
</style>

使用示例:

<template>
  <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick">
    <template #default="{ node }">
      <span @contextmenu.prevent="contextMenuRef!.openMenu($event, node)">
        {{ node.label }}
      </span>
    </template>
  </el-tree>
  <context-menu :menu-items="menuItems" ref="contextMenuRef" />
</template>
<script lang="ts" setup>
import { ref } from "vue";
import ContextMenu from "../components/contextMenu/ContextMenu.vue";
import { ContextMenuItem } from "../components/contextMenu/ContextMenu.vue";

interface Tree {
  label: string;
  children?: Tree[];
}

const contextMenuRef = ref<InstanceType<typeof ContextMenu>>();
const menuItems = ref<ContextMenuItem[]>([
  {
    name: "新增",
    action: (item: any) => {
      console.log(item, "新增");
    },
  },
  {
    name: "修改",
    action: (item: any) => {
      console.log(item, "修改");
    },
  },
  {
    name: "删除",
    action: (item: any) => {
      console.log(item, "删除");
    },
  },
]);

const handleNodeClick = (data: Tree) => {
  console.log(data);
};

const data: Tree[] = [
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1",
          },
        ],
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 3",
    children: [
      {
        label: "Level two 3-1",
        children: [
          {
            label: "Level three 3-1-1",
          },
        ],
      },
      {
        label: "Level two 3-2",
        children: [
          {
            label: "Level three 3-2-1",
          },
        ],
      },
    ],
  },
];

const defaultProps = {
  children: "children",
  label: "label",
};
</script>

参考文章:vue3实现右键菜单

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于在Vue 3中使用el-tree组件添加右键菜单,你可以按照以下步骤操作: 1. 首先,在Vue项目中安装并导入element-plus库,因为Vue 3不再支持使用element-ui。你可以使用以下命令安装element-plus: ``` npm install element-plus --save ``` 2. 在你的Vue组件中,导入el-tree组件和ContextMenu组件: ```javascript import { ElTree, ElContextMenu } from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; ``` 3. 在你的Vue组件中,添加el-tree组件和ContextMenu组件到template中,并设置相关的属性和事件监听器: ```html <template> <div> <el-context-menu :context-menu="contextMenu"> <el-tree :data="treeData" @node-contextmenu="handleContextMenu"></el-tree> </el-context-menu> </div> </template> <script> export default { data() { return { treeData: [ // 树节数据 ], contextMenu: [ // 右键菜单选项 ] }; }, methods: { handleContextMenu(node, event) { // 处理右键菜单击事件 } } } </script> ``` 4. 在data中定义treeData用于展示树节数据,contextMenu用于定义右键菜单选项。 5. 在handleContextMenu方法中,可以处理右键菜单击事件。你可以根据需要执行相关操作。 请注意,上述代码中的treeData和contextMenu需要根据实际需求进行修改和填充。你可以根据element-plus文档中的el-tree和ContextMenu组件配置选项,来调整和自定义右键菜单的样式和功能。 希望以上信息能够帮助到你!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值