关于有固定列的el-table 在滚动加载的时候固定列的行和非固定列的行对不齐有几px的错位且doLayout不生效有对应的解决方案:

1
在滚动结束时通过对比固定列table和非固定列table强制dom操作

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// @ts-ignore
import elInfiniteScroll from "element-ui/lib/infinite-scroll";
 
const elScope = "ElInfiniteScroll";
const msgTitle = `[el-table-infinite-scroll]: `;
const elTableScrollWrapperClass = ".el-table__body-wrapper";
 
// 处理滚动过程中固定列的行和非固定列的行不对齐的情况
function handleException( el: HTMLElement , scrollElem: HTMLElement, addEventLister: boolean = true) {
  let scrollTimer: any = null;
  function scrollCallback() {
    clearTimeout(scrollTimer);
    scrollTimer = setTimeout(() => {
      const scrollFixedElem = el.querySelector(".el-table__fixed-body-wrapper") as any;
      if (!scrollFixedElem) {
        return;
      }
      if (scrollFixedElem.scrollTop !== scrollElem.scrollTop) {
        scrollFixedElem.scrollTop = scrollElem.scrollTop;
      }
      const barHeight = 8;
      const tableHeaderHeight = 50;
      if (scrollElem.scrollHeight + barHeight === scrollElem.scrollTop + scrollElem.offsetHeight) {
        scrollFixedElem.style.top = tableHeaderHeight - barHeight + "px";
      } else {
        scrollFixedElem.style.top = tableHeaderHeight + "px";
      }
    }, 0);
  }
  if (addEventLister) {
    scrollElem.addEventListener("scroll", scrollCallback);
  } else {
    scrollElem.removeEventListener("scroll", scrollCallback);
  }
}
 
/**
 * 同步 el-infinite-scroll 的配置项
 * @param sourceVNode
 * @param sourceElem
 * @param targetElem
 */
function asyncElOptions(sourceVNode: any, sourceElem: any, targetElem: any) {
  let value;
  ["disabled", "delay", "immediate"].forEach((name: string) => {
    name = "infinite-scroll-" + name;
    value = sourceElem.getAttribute(name);
    if (value !== null) {
      targetElem.setAttribute(name, value);
    }
  });
 
  // fix: windows/chrome 的 scrollTop + clientHeight 与 scrollHeight 不一致的 BUG
  const name = "infinite-scroll-distance";
  value = sourceElem.getAttribute(name);
  targetElem.setAttribute(name, value < 1 ? 1 : value);
}
 
export default {
  inserted(el: any, binding: any, vnode: any, oldVnode: any) {
    // 获取 table 中的滚动层
    const scrollElem = el.querySelector(elTableScrollWrapperClass);
 
    // 如果没找到元素,返回错误
    if (!scrollElem) {
      throw `${msgTitle}找不到 ${elTableScrollWrapperClass} 容器`;
    }
 
    // 设置自动滚动
    scrollElem.style.overflowY = "auto";
 
    // dom 渲染后
    setTimeout(() => {
      // if (!el.style.height) {
      //   scrollElem.style.height = "400px";
      //   console.warn(
      //     `${msgTitle}请尽量设置 el-table 的高度,可以设置为 auto/100%(自适应高度),未设置会取 400px 的默认值(不然会导致一直加载)`
      //   );
      // }
 
      asyncElOptions(vnode, el, scrollElem);
 
      // 绑定 infinite-scroll
      elInfiniteScroll.inserted(scrollElem, binding, vnode, oldVnode);
 
      // 将子集的引用放入 el 上,用于 unbind 中销毁事件
      el[elScope] = scrollElem[elScope];
    }, 0);
 
    // 处理滚动过程中固定列的行和非固定列的行不对齐的情况
    handleException( el, scrollElem);
  },
  componentUpdated(el: any, binding: any, vnode: any) {
    asyncElOptions(vnode, el, el.querySelector(elTableScrollWrapperClass));
  },
  unbind: (el: any) => {
    try {
      const scrollElem = el.querySelector(elTableScrollWrapperClass);
      handleException( el, scrollElem, false);
      elInfiniteScroll.unbind();
    } catch (e) {
    }
  },
};

  

posted on   ygunoil  阅读(1377)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示