el-table setCurrentRow会触发current-change函数 解决方案

解决el-table setCurrentRow会触发current-change函数问题

javascript 复制代码
<template>
// 直接运行即可
  <template>
  <el-table
      ref="singleTableRef"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
      @current-change="handleCurrentChange"
  >
    <el-table-column type="index" width="50"/>
    <el-table-column property="date" label="Date" width="120"/>
    <el-table-column property="name" label="Name" width="120"/>
    <el-table-column property="address" label="Address"/>
  </el-table>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue';

const data = [
  { id: 1, date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  { id: 2, date: '2016-05-02', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  { id: 3, date: '2016-05-04', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  { id: 4, date: '2016-05-01', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
];

const singleTableRef = ref(null); // ref
const tableData = ref([]); // 表格数据
const isUpdating = ref(false); // 用于防止死循环的标识(重点)

// 高亮函数
const setCurrent = (row) => {
  if (singleTableRef.value && !isUpdating.value) {
    nextTick(() => {
      isUpdating.value = true; // 页面渲染完设置为true
      singleTableRef.value.setCurrentRow(row); // 执行高亮
      isUpdating.value = false; // 高亮之后设置为false(防止自执行current-change函数,如果不设置为false,那么就会去触发current-change函数)
    });
  }
};

const handleCurrentChange = (val) => {
  if (isUpdating.value || !val) return; // Prevent loop and handle null/undefined(防止循环和处理null/undefined)

  // Simulate async data update(模拟异步信息-)
  // setTimeout可以换做接口返回数据
  setTimeout(() => {
    const updatedData = [
      { id: 1, date: '2016-05-03', name: '小王', address: 'No. 189, Grove St, Los Angeles' },
      { id: 2, date: '2016-05-02', name: '小王', address: 'No. 189, Grove St, Los Angeles' },
      { id: 3, date: '2016-05-04', name: '小王', address: 'No. 189, Grove St, Los Angeles' },
      { id: 4, date: '2016-05-01', name: '小王', address: 'No. 189, Grove St, Los Angeles' }
    ];

    tableData.value = updatedData; // 用最新的数据或者深拷贝

    nextTick(() => {
      // find查找符合条件的
      const currentRow = updatedData.find(item => item.id === val.id);
      if (currentRow) {
        // 设置高亮
        setCurrent(currentRow);
      }
    });
  }, 1000);
};

onMounted(() => {
  // 这块设置默认选中一项
  setTimeout(() => {
    tableData.value = data;
    nextTick(() => {
      setCurrent(data[1]);
    });
  }, 800);
});
</script>
相关推荐
Mintopia2 分钟前
🤖 AI 决策 + 意图OS:未来软件形态的灵魂共舞
前端·人工智能·react native
攀登的牵牛花2 分钟前
前端向架构突围系列 - 框架设计(四):依赖倒置原则(DIP)
前端·架构
Van_Moonlight3 分钟前
RN for OpenHarmony 实战 TodoList 项目:已完成未完成数量显示
javascript·开源·harmonyos
程序员爱钓鱼10 分钟前
Node.js 编程实战:测试与调试 —— 日志与监控方案
前端·后端·node.js
计算机学姐16 分钟前
基于SpringBoot的汉服租赁系统【颜色尺码套装+个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·信息可视化·推荐算法
+VX:Fegn089517 分钟前
计算机毕业设计|基于springboot + vue建筑材料管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Mapmost19 分钟前
数字孪生项目效率翻倍!AI技术实测与场景验证实录
前端
小酒星小杜23 分钟前
在AI时代,技术人应该每天都要花两小时来构建一个自身的构建系统-Input篇
前端·程序员·架构
雪碧聊技术24 分钟前
ElementPlus徽章组件:展示日期面板每天未完成的待办数量
vue.js·日期选择器·elementplus·el-badge徽章组件
Cache技术分享31 分钟前
290. Java Stream API - 从文本文件的行创建 Stream
前端·后端