vue3 element-plus el-table表头冻结,表头吸顶

一.使用方式

在main.ts页面创建 vue指令

	import {
    createSticky } from '@/utils/sticky'
	const app = createApp(App)
	createSticky(app)
	...
	app.mount('#app');

在el-table标签上使用 v-sticky

<div class="table-box">
	<!--此处的 .table-box 是会出现滚动条的DOM元素通过document.querySelector选择器进行监听滚动条,如果不传则监听document.querySelector('body')的滚动条
	-->
	<el-table  v-sticky="{ top: 50, parent: '.table-box'}"        >
        ....
    </el-table>
<div>

通过以上使用方式,el-table就可以进行吸顶了。

二.脚本文件

复制并保存以下脚本内容到 utils文件夹。

/**
 * 思路:通过简体 el-table的 thead和tbody父级别区域,进行设置对于的fixed
 */

function getElParentBySelector(el: any, queryClassSelector: string) {
   
  if (!el) {
   
    return el
  }
  if ([...el.classList].includes(queryClassSelector)) {
   
    return el
  }
  return getElParentBySelector(el.parentNode, queryClassSelector)
}

function getTableShowWidth(thead: string) {
   
  const tableBox = getElParentBySelector(thead, 'el-table')
  return tableBox.getBoundingClientRect().width
}

function createTableSticky(el: any, binding: any, vNode?: any) {
   
  // 获取表格(element)
  let thead = el.querySelector('.el-table__header')
  thead = getElParentBySelector(thead, 'el-table__header-wrapper')
  const tbody = el.querySelector('.el-table__body')

  //获取thead 的显示宽度
  const headerShowWidth = getTableShowWidth(thead)

  // 获取滚动元素
  const scrollParent = document.querySelector(binding.value.parent||'body')
  if (!scrollParent || binding.value.disabled === true) {
   
    return
  }
  scrollParent.addEventListener('scroll', function () {
   
  	const stickyTop= binding.value.top||0;
    const theadHeight = thead.clientHeight
    // 获取thead距离顶部的距离
    const theadTop = thead.getBoundingClientRect().top
    if (theadTop <= stickyTop) {
   
      tbody.style.paddingTop = theadHeight + 'px'
      thead.style.position = 'fixed'
      thead.style.zIndex = '2021'
      thead.style.top = stickyTop + 'px'
      thead.style.borderTop = '1px solid #EBEBEB'

      //thead.style.width = tbody.offsetWidth + 'px' //

      //使用最佳显示宽度显示内容,防止有横向滚动条时,固定列显示超出
       thead.style.width =
        (tbody.offsetWidth < headerShowWidth ? tbody.offsetWidth : headerShowWidth) + 'px'
      

      //获取父级别的宽度,设置列头行只能是负极宽度
    }
    // 判断是否需要回归原来位置
    const originally = tbody.getBoundingClientRect().top
    // 判断底部距离是否超过表头
    const goBeyond = tbody.getBoundingClientRect().bottom
    if (originally > stickyTop || goBeyond <= thead.offsetHeight) {
   
      tbody.style.paddingTop = '0'
      thead.style.position = 'relative'
      thead.style.zIndex = '0'
      thead.style.top = 0 + 'px'
      thead.style.width = tbody.offsetWidth + 'px'
      thead.style.borderTop = 'none'
    }
  })
}

export function createSticky(vue: any) {
   
  let clearTimeId = 0
  // el-table表头吸顶效果
  vue.directive('sticky', {
   
    // 当被绑定的元素插入到 DOM 中时……
    mounted(el: any, binding: any) {
   
      //TIP 延时设置,确保表格进行渲染成功!
      clearTimeId = setTimeout(() => {
   
        createTableSticky(el, binding)
        clearTimeout(clearTimeId)
      }, 1000)
    },

    update(el: any, binding: any) {
   
      //TIP 延时设置,确保表格进行渲染成功!
      clearTimeId = setTimeout(() => {
   
        createTableSticky(el, binding)
        clearTimeout(clearTimeId)
      }, 1000)
    },

    unmounted(el: any, binding: any) {
   
      clearTimeId && clearTimeout(clearTimeId)
    }
  })
}

相关推荐

  1. vue3 element-plus el-table表头冻结表头

    2023-12-05 15:30:08       40 阅读
  2. Vue3 + TS + Element-Plus 封装的 Table 表格组件

    2023-12-05 15:30:08       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 15:30:08       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 15:30:08       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 15:30:08       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 15:30:08       18 阅读

热门阅读

  1. 聊聊logback的ThrowableProxyConverter

    2023-12-05 15:30:08       37 阅读
  2. 2022大厂高频面试题之HTML篇

    2023-12-05 15:30:08       43 阅读
  3. Spring-Mybatis读写分离笔记整理

    2023-12-05 15:30:08       26 阅读
  4. PTA 7-238 整数转换为字符串

    2023-12-05 15:30:08       36 阅读
  5. 2023-简单点-tkinter中的ttk和tk

    2023-12-05 15:30:08       41 阅读
  6. RabbitMQ避免重复消费

    2023-12-05 15:30:08       57 阅读
  7. 简谈PostgreSQL的wal_level=logic

    2023-12-05 15:30:08       31 阅读
  8. FlinkSql-Temporal Joins-Lookup Join

    2023-12-05 15:30:08       46 阅读
  9. postgresql树状结构查询示例

    2023-12-05 15:30:08       41 阅读