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

3 篇文章 1 订阅

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

npm install @xunlei/vue-context-menu --save

<div id="dataPage">
  <el-tree
    id="el-tree"
    :data="flowTree.root"
    :props="flowTree.props"
    @node-click="selectFlow"
    @node-contextmenu="rightClick"/>
  <vue-context-menu
    :target="contextMenuTarget"
    :show="contextMenuVisible"
    class="right-menu"
    @update:show="(show) => contextMenuVisible = show">
    <a
      href="javascript:;"
      @click="createDatabaseOrTable">新建</a>
    <a
      href="javascript:;"
      @click="deleteDatabaseOrTable">删除</a>
  </vue-context-menu>
</div>

import { component as VueContextMenu } from '@xunlei/vue-context-menu';
components: {
  'vue-context-menu': VueContextMenu,
},
data(){
  return {
  	flowTree: {
      root: [],
      props: {
        label: 'name',
        children: 'children',
      },
    },
    treeNodeData: '', // 存当前数据
    treeNode: '', // 存当前节点信息
    contextMenuVisible: false, // 让菜单显示
    contextMenuTarget: null,
  }
},
methods: {
  rightClick(e, data, node) {
      this.treeNodeData = data;// 存当前数据
      this.treeNode = node;// 存当前节点信息
      console.log('rightClick', this.treeNodeData, this.treeNode);
      this.contextMenuVisible = true;// 让菜单显示
      console.log('0', e, '1', e.screenX, '2', e.screenY);
      const ele = document.querySelector('.right-menu');
      ele.style.position = 'fixed';
      ele.style.top = `${e.clientY}px`;
      ele.style.left = `${e.clientX + 10}px`; //根据鼠标点击位置设置菜单出现
  },
  createDatabaseOrTable() {
    this.contextMenuVisible = false;
    console.log('createDatabaseOrTable', this.treeNodeData, this.treeNode);
  },
  deleteDatabaseOrTable() {
    this.contextMenuVisible = false;
    console.log('deleteDatabaseOrTable', this.treeNodeData, this.treeNode);
  },
}

<style lang="less">
	#dataPage {
    margin: 0 0;
    text-align: center;
    color: #2c3e50;
    height: 100%;
  }
  // 右键会选中文字,为了美观将它禁用
  #el-tree{
    user-select:none;
  }
  .right-menu {
    font-size: 14px;
    position: fixed;
    background: #fff;
    border: solid 1px rgba(0, 0, 0, .2);
    border-radius: 3px;
    z-index: 999;
    display: none;
  }
  .right-menu a {
    width: 150px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    display: block;
    color: #1a1a1a;
    padding: 2px;
  }
  .right-menu a:hover {
    background: #bbb;
    color: #fff;
  }
  .right-menu {
    border: 1px solid #eee;
    box-shadow: 0 0.5em 1em 0 rgba(0,0,0,.1);
    border-radius: 1px;
  }
  a {
    text-decoration: none;
  }
</style>

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

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现消息通知,可以借助VueElementUI和Websockets,以下是基本的实现步骤: 1. 安装VueElementUI 使用命令行工具,进入项目目录,执行以下命令: ``` npm install vue npm install element-ui ``` 2. 引入ElementUI组件Vue的入口文件中,引入需要使用的ElementUI组件,例如Notification、MessageBox等。 ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建WebSocket连接 在Vue组件中通过Websockets连接后端服务器,建立WebSocket连接,接收服务器推送的消息。 ``` // 建立WebSocket连接 const webSocket = new WebSocket('ws://localhost:8080') // 监听WebSocket连接打开事件 webSocket.onopen = function () { console.log('WebSocket连接已打开') } // 监听WebSocket接收到消息事件 webSocket.onmessage = function (event) { console.log('WebSocket接收到消息:' + event.data) // 处理接收到的消息 } // 监听WebSocket连接关闭事件 webSocket.onclose = function () { console.log('WebSocket连接已关闭') } ``` 4. 处理接收到的消息 在WebSocket监听到有消息时,可以使用ElementUI的Notification组件弹出通知框,提醒用户有新消息。 ``` // 处理WebSocket接收到的消息 webSocket.onmessage = function (event) { console.log('WebSocket接收到消息:' + event.data) // 弹出通知框 const msg = JSON.parse(event.data) Notification({ title: '新消息', message: msg.content }) // 弹出确认框 MessageBox.confirm(msg.content, '新消息', { confirmButtonText: '查看详情', cancelButtonText: '忽略', type: 'info' }).then(() => { // 用户点击了查看详情按钮 // 处理跳转逻辑 }).catch(() => { // 用户点击了忽略按钮 // 不做任何处理 }) } ``` 以上就是使用VueElementUI和Websockets实现消息通知的基本步骤。需要注意的是,WebSocket连接的建立和关闭需要在组件的mounted和beforeDestroy生命周期函数中分别进行。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值