el-table setCurrentRow会触发current-change函数 办理方案

打印 上一主题 下一主题

主题 1033|帖子 1033|积分 3099

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
办理el-table setCurrentRow会触发current-change函数题目

  1. <template>
  2. // 直接运行即可
  3.   <template>
  4.   <el-table
  5.       ref="singleTableRef"
  6.       :data="tableData"
  7.       highlight-current-row
  8.       style="width: 100%"
  9.       @current-change="handleCurrentChange"
  10.   >
  11.     <el-table-column type="index" width="50"/>
  12.     <el-table-column property="date" label="Date" width="120"/>
  13.     <el-table-column property="name" label="Name" width="120"/>
  14.     <el-table-column property="address" label="Address"/>
  15.   </el-table>
  16. </template>
  17. <script setup>
  18. import { ref, onMounted, nextTick } from 'vue';
  19. const data = [
  20.   { id: 1, date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  21.   { id: 2, date: '2016-05-02', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  22.   { id: 3, date: '2016-05-04', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  23.   { id: 4, date: '2016-05-01', name: 'Tom', address: 'No. 189, Grove St, Los Angeles' },
  24. ];
  25. const singleTableRef = ref(null); // ref
  26. const tableData = ref([]); // 表格数据
  27. const isUpdating = ref(false); // 用于防止死循环的标识(重点)
  28. // 高亮函数
  29. const setCurrent = (row) => {
  30.   if (singleTableRef.value && !isUpdating.value) {
  31.     nextTick(() => {
  32.       isUpdating.value = true; // 页面渲染完设置为true
  33.       singleTableRef.value.setCurrentRow(row); // 执行高亮
  34.       isUpdating.value = false; // 高亮之后设置为false(防止自执行current-change函数,如果不设置为false,那么就会去触发current-change函数)
  35.     });
  36.   }
  37. };
  38. const handleCurrentChange = (val) => {
  39.   if (isUpdating.value || !val) return; // Prevent loop and handle null/undefined(防止循环和处理null/undefined)
  40.   // Simulate async data update(模拟异步信息-)
  41.   // setTimeout可以换做接口返回数据
  42.   setTimeout(() => {
  43.     const updatedData = [
  44.       { id: 1, date: '2016-05-03', name: '小王', address: 'No. 189, Grove St, Los Angeles' },
  45.       { id: 2, date: '2016-05-02', name: '小王', address: 'No. 189, Grove St, Los Angeles' },
  46.       { id: 3, date: '2016-05-04', name: '小王', address: 'No. 189, Grove St, Los Angeles' },
  47.       { id: 4, date: '2016-05-01', name: '小王', address: 'No. 189, Grove St, Los Angeles' }
  48.     ];
  49.     tableData.value = updatedData; // 用最新的数据或者深拷贝
  50.     nextTick(() => {
  51.       // find查找符合条件的
  52.       const currentRow = updatedData.find(item => item.id === val.id);
  53.       if (currentRow) {
  54.         // 设置高亮
  55.         setCurrent(currentRow);
  56.       }
  57.     });
  58.   }, 1000);
  59. };
  60. onMounted(() => {
  61.   // 这块设置默认选中一项
  62.   setTimeout(() => {
  63.     tableData.value = data;
  64.     nextTick(() => {
  65.       setCurrent(data[1]);
  66.     });
  67.   }, 800);
  68. });
  69. </script>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王海鱼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表