一个用于实现 Element UI (Vue 2) 表格的表头吸顶、表尾吸底、滚动条吸底以及高度自适应功能的指令集插件

Element UI 的表格组件在使用时,如果表格内容过多,表格会出现滚动条,但是表头不会吸顶,表尾合计行也不会吸底,这样在表格内容过多时,表头和表尾合计行就会被遮挡,如果有横向滚动条,每次想滚动必须滑动到最底下,这一系列痛点,非常影响用户体验。

Element UI 的解决办法:通过设置 max-height 或者 height 属性来实现上述需求。

Element UI 的解决不足点:高度值只支持设置数字型,在实际开发中往往需要借助 JS 来计算。

部分指令依赖于 position: sticky 属性,所以只支持现代浏览器,在线示例

  • [x] 支持表头吸顶 (v-sticky-header)
  • [x] 支持表尾合计行吸底 (v-sticky-footer)
  • [x] 支持横向滚动条吸底 (v-sticky-scroller)
  • [x] 支持高度自适应 (v-height-adaptive)

安装

npm install @cell-x/el-table-sticky

注册指令

全局注册指令:

 
import elTableSticky from '@cell-x/el-table-sticky'

Vue.use(elTableSticky)

// 或者

Vue.use(elTableSticky, {
  StickyHeader: {
    // 吸顶偏移量,可以是 CSS 支持的距离值,如 `0px`、`10%`、`calc(100vh - 1rem)` 等
    offsetTop: 0,
    // 滚动条吸底偏移量,可以是 CSS 支持的距离值,如 `0px`、`10%`、`calc(100vh - 1rem)` 等
    offsetBottom: 0,
  },
  StickyFooter: {
    // 吸底偏移量,可以是 CSS 支持的距离值,如 `0px`、`10%`、`calc(100vh - 1rem)` 等
    offsetBottom: 0,
  },
  StickyScroller: {
    // 吸底偏移量,可以是 CSS 支持的距离值,如 `0px`、`10%`、`calc(100vh - 1rem)` 等
    offsetBottom: 0,
  },
  HeightAdaptive: {
    // 底部偏移量,只能是数字型
    offsetBottom: 0,
  }
})

局部注册指令:

import {
  StickyHeader,
  StickyFooter,
  StickyScroller,
  HeightAdaptive,
} from '@cell-x/el-table-sticky'

export default {
  directives: {
    StickyHeader: new StickyHeader({ offsetTop: 0, offsetBottom: 0 }).init(),
    StickyFooter: new StickyFooter({ offsetBottom: 0 }).init(),
    StickyScroller: new StickyScroller({ offsetBottom: 0 }).init(),
    HeightAdaptive: new HeightAdaptive({ offsetBottom: 0 }).init(),
  }
}

使用

<el-table v-sticky-header>...</el-table>
<el-table v-sticky-footer>...</el-table>
<el-table v-sticky-scroller>...</el-table>
<el-table v-height-adaptive>...</el-table>

https://www.npmjs.com/package/@cell-x/el-table-sticky/v/1.0.5?activeTab=readme

 案例源码: https://github.com/Lruihao/el-table-sticky.git

 
 
 
posted @ 2025-06-14 10:50  瞎BB的是2B  阅读(199)  评论(0)    收藏  举报