elementUI el-table实现鼠标悬浮某一行,在鼠标右侧展示提示信息

背景

el-table组件中,可以通过勾选某条数据来创建单据,但是有些数据没有权限使用,就需要禁用掉勾选的功能,然后当鼠标悬浮在这一行的时候,展示类似于toolTip的提示框。

除了当鼠标悬浮在某一行,展示类似于toolTip的提示框这一条el-table是没有提供配置项的,其他的都能够通过配置完成,那我们接下来看看如何实现鼠标悬浮某一行展示提示框的需求。

实现效果

具体实现

首先el-table有提供两个事件cell-mouse-enter和cell-mouse-leave,这两个事件分别在当单元格 hover 进入时以及当单元格 hover 退出时会触发,回调函数中能接收四个参数:row, column, cell, event。

我们可以通过cell-mouse-enter事件,在鼠标进入到当前行的时候,根据第一个参数row判断当前行是否需要进行提示。如果需要提示的话,我们可以获取第四个参数event,拿到当前触发hover事件的dom元素。然后动态生成一个提示框div定位到鼠标右侧,插入到body中。

然后通过监听cell-mouse-leave事件将这个元素从body中移除。

代码如下

html 复制代码
// table组件
<el-table 
 :data="tableData" 
 style="width: 100%"
 @cell-mouse-enter="enterSelectionRows"
 @cell-mouse-leave="leaveSelectionRows"
>
   // ......
</el-table>
javascript 复制代码
// 鼠标进入表格行的回调函数
enterSelectionRows: (row:any, column:any, cell:any, event:any) => {
  if (!row.hasAuth) {
       createTips(event, row, '请先在资产平台申请对应表查询权限')
        return
     }
 }
// 鼠标离开表格行的回调函数
leaveSelectionRows: (row:any) => {
      removeTips(row)
}


// 创建toolTip
export function createTips(el:any, row:any, value:any) {
  const { id } = row
  const tooltipDom = document.createElement('div')
  tooltipDom.style.cssText = `
        display: inline-block;
        max-width: 400px;
        max-height: 400px;
        position: absolute;
        top: ${el.clientY + 5}px;
        left: ${el.clientX}px;
        padding:5px 10px;
        overflow: auto;
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #595959;
        background: #fff;
        border-radius: 5px;
        z-index: 19999;
        box-shadow: 0 4px 12px 1px #ccc;
      `
  tooltipDom.innerHTML = value
  tooltipDom.setAttribute('id', `tooltip-${id}`)
  // 将浮层插入到body中
  document.body.appendChild(tooltipDom)
}

// 删除tooltip
export function removeTips(row:any) {
  const { id } = row
  const tooltipDomLeave = document.querySelectorAll(`#tooltip-${id}`)
  if (tooltipDomLeave.length) {
    tooltipDomLeave.forEach(dom => {
      document.body.removeChild(dom)
    })
  }
相关推荐
大团米线10 小时前
vite多环境合并打包config
javascript·vue.js·ecmascript
晚风叙码10 小时前
Linux 进程概念(上):从冯诺依曼到进程调度
linux·运维·计算机外设
定栓11 小时前
Vite-legacy产物console删除机制
前端·javascript·vue.js
康小汪19 小时前
AI 写 Vue 总写成同一个味道:给它装 Skills,装的不是知识是边界
前端·vue.js·人工智能·ai编程·最佳实践
北冥有鱼1 天前
发版后动态路由点击报 `Failed to fetch dynamically imported module`」问题上的完整排查与修复过程
前端·vue.js
apollo_qwe1 天前
大模型 API 接通了,AI 聊天页面为什么还是不好用?
前端·vue.js·人工智能
科技每日热闻1 天前
爱攻AGON亮相2026核聚变游戏嘉年华北京站,创新科技点燃高能电竞热潮!
科技·计算机外设
小郎君6581 天前
keep-alive 页面怎么“真刷新”?别改外层 key——一个 refreshVersion 的完整实现
vue.js·算法
徐小夕1 天前
JitWord 4.3 发布!Word 内嵌 Visio 图表、原生流程图一键无损解析
前端·vue.js·github
king王一帅1 天前
做了三年 ProseMirror 后,我用四个月自研排版引擎实现 WPS / Google Docs 替代品,整套框架 MIT 开源
前端·vue.js·开源