基于 react-window 实现虚拟滚动 table 的固定行列功能

问题描述

react-window 是一个用于高效呈现大型列表和表格数据的 React 库 但是这个库本身并不直接支持固定表格行列 如果在使用 react-window 的时候存在固定行列需求,我们只能自己完成封装

固定行列虚拟 table 效果类似于这样:

实现原理 - 官方 demos & issues

如果要了解在 react-window 中如何实现固定头列(sticky 布局),可以阅读下面资料:

Does this library support "sticky" items?

Fixed grid headers with CSS position sticky #166

需求描述

  1. 基于 react-window 实现一个 table 的功能
  2. 该 table 能够实现4 个方向上(top, right, bottom, left) 固定行列(通过参数控制)

实现

9 区块的图示

table 的场景下,如果需要实现 4 个方向上(top, right, bottom, left)的 sticky 布局,那么展示应该如下图所示

区域说明:

1, 2, 3, 4\]: z-index 为最大值的区域,该区域不会随着滚动而发生内容的变化 \[5, 6, 7, 8\]: z-index 处于中间值的区域,5,7 不会随着 x 轴的滚动而发变化;6,8 不会随着 y 轴的滚动而发生变化 \[content\]: z-index 为最小值的区域,是 table 的非 sticky 区域 ## 9 区块的 html 实现 这种多区块布局 + sticky 布局,直接封装组件很容易出现问题,所以这里我们先使用 html + css 完成最基础的布局,然后才进行组件的开发 [code url - 6_sticky布局9区块的实现](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2FouleWorld%2Foulae_blog_warehouse%2Fblob%2Fmain%2Fmarkdown%2F%25E8%2599%259A%25E6%258B%259F%25E6%25BB%259A%25E5%258A%25A8%2Fdemos%2F6_sticky%25E5%25B8%2583%25E5%25B1%25809%25E5%258C%25BA%25E5%259D%2597%25E7%259A%2584%25E5%25AE%259E%25E7%258E%25B0.html "https://github.com/ouleWorld/oulae_blog_warehouse/blob/main/markdown/%E8%99%9A%E6%8B%9F%E6%BB%9A%E5%8A%A8/demos/6_sticky%E5%B8%83%E5%B1%809%E5%8C%BA%E5%9D%97%E7%9A%84%E5%AE%9E%E7%8E%B0.html") 页面效果: ![image.png](https://file.jishuzhan.net/article/1705785161620131841/1faa8b4d653aa1aaa73a8d953c0a21fb.webp) ## 组件的封装 封装思路:计算 \[1, 2, 3, 4, 5, 6, 7, 8, content\] 所有容器的需要展示的元素,然后将这些元素塞入到预定好的 DOM 中 封装难点:8 个 sticky 元素的 css 布局实现起来比较麻烦 组件的代码和文档可以参照下面两个链接: [code - ReactWindowStickyCells 组件代码](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2FouleWorld%2Foulae_dumi_component_web%2Fblob%2Fmain%2Fsrc%2FReactWindowStickyCells%2Findex.jsx "https://github.com/ouleWorld/oulae_dumi_component_web/blob/main/src/ReactWindowStickyCells/index.jsx") [ReactWindowStickyCells 组件文档](https://link.juejin.cn?target=https%3A%2F%2Fouleworld.github.io%2Foulae_dumi_component_web%2Fcomponent%2Freact-window-sticky-cells%23reactwindowstickycells "https://ouleworld.github.io/oulae_dumi_component_web/component/react-window-sticky-cells#reactwindowstickycells") ![reactwindow.gif](https://file.jishuzhan.net/article/1705785161620131841/6e9ee35ce5a31f0ac9ca8e9cebe825e6.webp) **如果需要理解组件的具体实现,推荐先看完参考资料,理解 react-window 的基本原理** **如果想要弄清楚实现细节,这里可以看下组件的实现细节,代码阅读难度不高,这里最难的地方其实这个 css 布局的处理** # 参考链接 1. [Does this library support "sticky" items?](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fbvaughn%2Freact-window%23does-this-library-support-sticky-items "https://github.com/bvaughn/react-window#does-this-library-support-sticky-items") 2. [Fixed grid headers with CSS position sticky #166](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fbvaughn%2Freact-window%2Fissues%2F166 "https://github.com/bvaughn/react-window/issues/166") 3. [code - ReactWindowStickyCells 组件代码](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2FouleWorld%2Foulae_dumi_component_web%2Fblob%2Fmain%2Fsrc%2FReactWindowStickyCells%2Findex.jsx "https://github.com/ouleWorld/oulae_dumi_component_web/blob/main/src/ReactWindowStickyCells/index.jsx") 4. [ReactWindowStickyCells 组件文档](https://link.juejin.cn?target=https%3A%2F%2Fouleworld.github.io%2Foulae_dumi_component_web%2Fcomponent%2Freact-window-sticky-cells%23reactwindowstickycells "https://ouleworld.github.io/oulae_dumi_component_web/component/react-window-sticky-cells#reactwindowstickycells") 5. [虚拟滚动 demo 相关](https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2FouleWorld%2Foulae_blog_warehouse%2Ftree%2Fmain%2Fmarkdown%2F%25E8%2599%259A%25E6%258B%259F%25E6%25BB%259A%25E5%258A%25A8%2Fdemos%2F7_react_window_%25E5%259B%25BA%25E5%25AE%259A%25E8%25A1%258C%25E5%2588%2597%25E8%25A1%25A8%25E6%25A0%25BC "https://github.com/ouleWorld/oulae_blog_warehouse/tree/main/markdown/%E8%99%9A%E6%8B%9F%E6%BB%9A%E5%8A%A8/demos/7_react_window_%E5%9B%BA%E5%AE%9A%E8%A1%8C%E5%88%97%E8%A1%A8%E6%A0%BC")

相关推荐
CF14年老兵9 分钟前
🎮 把 GitHub 变成游戏厅!(吃豆人 + 贪吃蛇 + 炫酷奖杯!)
前端·后端·trae
Dorcas_FE13 分钟前
【tips】css模仿矢量图透明背景
前端·css
Casta-mere26 分钟前
React SSR 水合问题
前端·react.js·前端框架·ssr
一点一木40 分钟前
Trae 实测系列 #1:30 分钟用中文 AI 编程搞定 Vue3 + Pinia 倒计时应用
前端·ai编程·trae
野区小女王40 分钟前
React函数组件灵魂搭档:useEffect深度通关指南!
前端·react.js·前端框架
我是火山呀41 分钟前
React+TypeScript代码注释规范指南
前端·react.js·typescript
JefferyXZF1 小时前
Next.js 路由处理程序:前端也能轻松玩转后端 API(五)
前端·全栈·next.js
阳先森1 小时前
vue 数据更新到视图变化全过程
前端·vue.js
Sobeit1 小时前
Vue 3.5 响应式设计与实现流程全解析
前端·vue.js
蓝倾9761 小时前
唯品会以图搜图(拍立淘)API接口调用指南详解
java·大数据·前端·数据库·开放api接口