Bang Hu
1 天以前 8a709ba6db50831048f9c3e2452ea6dc6c3de36f
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
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wuyun
 * @Date: 2022-12-05 14:04:33
 * @LastEditors: wuyun
 * @LastEditTime: 2024-09-03 15:27:18
 */
/**
 * v-tableHeight
 * 动态计算表格高度
 */
 
import { Directive } from 'vue'
import type { DirectiveBinding } from 'vue'
import _ from 'lodash'
export const tableHeight: Directive = {
  mounted(el: any, binding: DirectiveBinding) {
    el.tableHeight = _.debounce(() => {
      const contentHeight = window.innerHeight //容器的高度
      const topHeight = el.getBoundingClientRect().top //表格顶部离body的距离
      const pageinationHeight: any = document.querySelector('.paginationDiv')
        ? document.querySelector('.paginationDiv')?.clientHeight
        : 0
      el.style.height =
        contentHeight - topHeight - pageinationHeight - 51 + 'px' //50是表头的高度 10 是表格和底部的间距
    })
    window.addEventListener('resize', el.tableHeight)
  },
  updated: (el, binding) => {
    el.tableHeight()
  },
  beforeUnmount: (el) => {
    window.removeEventListener('resize', el.tableHeight)
  },
}
export default tableHeight