element ui tree结构双击单机右键事件

<el-tree
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
          @node-contextmenu="rightClick"
          default-expand-all
        >
        </el-tree>


<!-- 右键菜单 -->
    <div id="menuyuan" class="menuDiv">
      <ul class="menuUl">
        <li
          v-for="(item, index) in menus"
          :key="index"
          @click.stop="infoClick(index)"
        >
          <i :class="item.icon"></i> {{ item.name }}
        </li>
      </ul>
    </div>

1、以上是html  包括右键菜单

2、data中

      //双击
      nodeCount: 0,
      preNodeId: null,
      curNodeId: null,
      nodeTimer: null,
      
       //右键
      currentData: "",
      currentNode: "",
      menuVisible: false,
      firstLevel: false,
      Node: {},

      menus: [
        { name: "菜单一", operType: 1, icon: "el-icon-tickets" },
        { name: "二", operType: 2, icon: "el-icon-folder-opened" },
        { name: "三", operType: 3, icon: "el-icon-folder-add" },
        { name: "四", operType: 4, icon: "el-icon-refresh-right" },
        { name: "五", operType: 5, icon: "el-icon-close" },
      ],

3、methods中

    //双击
    handleNodeClick(data, node, prop) {
      //这里可以调用单击事件
        //这是单击隐藏右键菜单
      let menu = document.querySelector("#menuyuan");
      menu.style.display = "none";


      // console.log(data, node, prop);
      this.nodeCount++;
      if (this.preNodeId && this.nodeCount >= 2) {
        this.curNodeId = data.$treeNodeId;
        this.nodeCount = 0;
        if (this.curNodeId == this.preNodeId) {
          this.compile(node);
          this.curNodeId = null;
          this.preNodeId = null;
          return;
        }
      }
      this.preNodeId = data.$treeNodeId;
      this.nodeTimer = setTimeout(() => {
        this.preNodeId = null;
        this.nodeCount = 0;
      }, 300);
    },


// 右击的功能
rightClick(event, object, Node, element) {
      this.Node = Node
      this.currentData = object;
      this.currentNode = Node;
      if (Node.level === 1) {
        this.firstLevel = true;
      } else {
        this.firstLevel = false;
      }
      this.menuVisible = true;
      let menu = document.querySelector('#menuyuan')
      // /* 菜单定位基于鼠标点击位置 */
      menu.style.left = event.clientX + 20 + "px";
      menu.style.top = event.clientY - 25 + "px";
      menu.style.display = "block";
    },
    // 右击自定义菜单的点击事件
    infoClick(index) {
      if (index === 0) {
        // 属性
        console.log("属性");
      }else if(index === 1){
        
      }else if(index === 2){
        
      }else if(index === 3){
       
      }else if(index === 4){
        
      }
      let menu = document.querySelector("#menuyuan");
      menu.style.display = "none";
    },

4、给右键菜单加个样式

.menuDiv {
    display: none;
    position: absolute;
    .menuUl {
      height: auto;
      width: auto;
      font-size: 14px;
      text-align: left;
      border-radius: 4px;
      border: none;
      background-color: #ffffff;
      color: #606266;
      list-style: none;
      border: 1px solid #ebeef5;
      li {
        width: 140px;
        height: 35px;
        line-height: 35px;
        padding: 0 10px;
        cursor: pointer;
        border-bottom: 1px solid rgba(255, 255, 255, 0.47);
        &:hover {
          display: block;
          background-color: #ecf5ff;
          color: #7abbff;
        }
      }
    }
  }

个人使用的一些总结,使用中发现问题随时反馈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值