解决 el-table 在 fixed 状态下获取 dom 不准确的问题

关键词: vue el-table fixed 焦点 dom

近日有需求是 el-table 中焦点跳转,回车跳转到同一列下一行,一列结束之后回到下一列第一行。

如图所示:

开始一切正常,通过使用 :ref="getActualValueInputRef(scope.$index, index)" 可以设置对应的 ref ,并通过 this.$refs\[XXX] 获取对应的 dom

javascript 复制代码
<el-input
  v-if="!readonly"
  :ref="getActualValueInputRef(scope.$index, index)"
  v-model="scope.row.valueActuals[index - 1]"
  :placeholder="getActualValueLabel(index)"
  size="small"
  @keydown.enter.native="handleActualValueEnter(scope.$index, index, $event)"
  @input="handleValueActualChange(scope.row, index - 1, $event)"
/>


// 生成实测值输入框的 ref 名称
getActualValueInputRef(rowIndex, colIndex) {
  return `actualValueInput_${rowIndex}_${colIndex}`
},

// 聚焦到指定的实测值单元格
focusActualValueCell(rowIndex, colIndex) {
    const refName = this.getActualValueInputRef(rowIndex, colIndex)
    const inputComponents = this.$refs[refName]
    const targetComponent = inputComponents[inputComponents.length - 1]
    if (targetComponent) {
      // 直接操作原生 input 元素
      const inputElement = targetComponent.$el?.querySelector('input') || targetComponent.$refs?.input
      if (inputElement) {
        inputElement.focus()
        // 延迟选中,确保焦点已设置
        setTimeout(() => {
          inputElement.select()
        }, 0)
      }
    }
}

但由于 el-table 设置了 fixed 所以导致获取的 inputComponents 是一个数组 ,一开始直接获取数组最后一个元素即可:inputComponents\[inputComponents.length - 1]

但后面新增行的时候发现无法正确获取焦点。调试之后发现焦点跳转到了 fixed 复制的副本元素中。即默认渲染的 inputComponents 中最后一个元素是页面上展示的元素。但新增的行中 inputComponents 的最后一个元素是 fixed 的副本,第一个元素才是页面上展示的元素。

解决方案:

javascript 复制代码
// 判断组件是否位于display区域(非fixed区域)
isDisplayAreaComponent(component) {
  if (!component || !component.$el) return false

  // 检查组件是否在非fixed的表格中
  const tableEl = component.$el.closest('.el-table')
  if (!tableEl) return false

  // 检查是否在fixed列中
  const isFixedLeft = component.$el.closest('.el-table__fixed') // 左固定
  const isFixedRight = component.$el.closest('.el-table__fixed-right') // 右固定

  // 如果不在任何fixed区域,则认为是display区域的组件
  return !isFixedLeft && !isFixedRight
},

// 聚焦到指定的实测值单元格
focusActualValueCell(rowIndex, colIndex) {
    const refName = this.getActualValueInputRef(rowIndex, colIndex)
    const inputComponents = this.$refs[refName]
    
    let targetComponent = null
    // 首先尝试找到display区域的组件(非fixed区域)
    for (const component of inputComponents) {
      if (this.isDisplayAreaComponent(component)) {
        targetComponent = component
        break
      }
    }

    // 如果还是没找到,使用最后一个组件作为后备方案
    if (!targetComponent) {
      targetComponent = componentArray[componentArray.length - 1]
    }
    
    if (targetComponent) {
      // 直接操作原生 input 元素
      const inputElement = targetComponent.$el?.querySelector('input') || targetComponent.$refs?.input
      if (inputElement) {
        inputElement.focus()
        // 延迟选中,确保焦点已设置
        setTimeout(() => {
          inputElement.select()
        }, 0)
      }
    }
}

通过遍历 inputComponents 元素使用 isDisplayAreaComponent 来判断该元素是否是 fixed 复制出来的副本,只获取真实的页面的元素并跳转焦点。

至此该问题完全解决。关键是通过 $el.closest() 来判断是否是 fixed 副本。

closest() 是一个 DOM 方法,从当前元素开始,沿 DOM 树向上查找匹配指定选择器的最近的祖先元素(包括元素本身)。例如:

  • element.closest('.el-table') 会从当前元素开始向上查找,返回第一个具有 .el-table 类的祖先元素

通过查找父元素是否存在样式 .el-table__fixed,.el-table__fixed-right 判断该元素是否是 el-table 因为 fixed 创建的副本,从而来找到页面上展示的 dom 。

相关推荐
Python自动化直播17 分钟前
智能脚本批量管理拆开看:导入、导出、重写、试听
前端·python·ai·秒播
开开心心就好33 分钟前
发票信息录入太麻烦?离线工具一键转表格
运维·服务器·前端·pdf·ocr·maven·intellij-idea
এ慕ོ冬℘゜1 小时前
ES Promise.all/ Promise.race
前端·javascript·elasticsearch
喜欢打篮球的普通人2 小时前
LLVM 前端与优化层:Clang 如何把 C 代码变成 IR
c语言·前端·状态模式
大文说跨境2 小时前
亚马逊多店铺用过多款指纹浏览器,说点长期用的实际感受
前端·网络
中屹指纹浏览器svip2 小时前
中屹指纹浏览器和普通 Chrome 有什么区别?
前端·chrome·中屹指纹浏览器
feixing_fx3 小时前
CSS Grid 入门:二维布局时代的降维打击
前端·css·前端框架·css3
Lank_M3 小时前
JPG的8×8块里只要有条边,白底就保不住255
前端
coding漫漫长路3 小时前
主图纯白检测,阈值放宽到250会放过252的底
前端