wangzhibo
2026-07-16 b57020623cf0c946706573bf102e548c3a544423
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
import { debounce, last } from "lodash-es";
import { nextTick, onActivated, onMounted, ref } from "vue";
import { addClass, removeClass } from "../../../utils";
import { mitt } from "../../../utils/mitt";
 
// 表格高度
export function useHeight({ config, Table }: { Table: Vue.Ref<any>; config: ClTable.Config }) {
    // 最大高度
    const maxHeight = ref(0);
 
    // 计算表格最大高度
    const update = debounce(async () => {
        await nextTick();
 
        let vm = Table.value;
 
        if (vm) {
            while (!vm.$parent?.$el.className?.includes("cl-crud")) {
                vm = vm.$parent;
            }
 
            if (vm) {
                const p = vm.$parent.$el;
 
                await nextTick();
 
                // 高度
                let h = 0;
 
                // 表格下间距
                if (vm.$el.className.includes("cl-row")) {
                    h += 10;
                }
 
                // 上高度
                h += vm.$el.offsetTop;
 
                // 获取下高度
                let n = vm.$el.nextSibling;
 
                // 集合
                const arr = [vm.$el];
 
                while (n) {
                    if (n.offsetHeight > 0) {
                        h += n.offsetHeight || 0;
                        arr.push(n);
 
                        if (n.className.includes("cl-row--last")) {
                            h += 10;
                        }
                    }
 
                    n = n.nextSibling;
                }
 
                // 移除 cl-row--last
                arr.forEach((e) => {
                    removeClass(e, "cl-row--last");
                });
 
                // 最后一个可视元素
                const z = last(arr);
 
                // 去掉 cl-row 下间距高度
                if (z?.className.includes("cl-row")) {
                    addClass(z, "cl-row--last");
                    h -= 10;
                }
 
                // 上间距
                h += parseInt(window.getComputedStyle(p).paddingTop, 10);
 
                // 设置最大高度
                if (config.autoHeight) {
                    maxHeight.value = p.clientHeight - h;
                }
            }
        }
    }, 100);
 
    // 窗口大小改变事件
    mitt.on("resize", () => {
        update();
    });
 
    onMounted(function () {
        update();
    });
 
    onActivated(function () {
        update();
    });
 
    return {
        maxHeight,
        calcMaxHeight: update
    };
}