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
import { nextTick, ref } from "vue";
import { useCore } from "../../../hooks";
import { isArray, isBoolean } from "lodash-es";
 
export function useOp({ config }: { config: ClTable.Config }) {
    const { mitt } = useCore();
 
    // 是否可见,用于解决一些显示隐藏的副作用
    const visible = ref(true);
 
    // 重新构建
    async function reBuild(cb?: fn) {
        visible.value = false;
 
        await nextTick();
 
        if (cb) {
            cb();
        }
 
        visible.value = true;
 
        await nextTick();
 
        mitt.emit("resize");
    }
 
    // 显示列
    function showColumn(prop: string | string[], status?: boolean) {
        const keys = isArray(prop) ? prop : [prop];
 
        // 多级表头
        function deep(list: ClTable.Column[]) {
            list.forEach((e) => {
                if (e.prop && keys.includes(e.prop)) {
                    e.hidden = isBoolean(status) ? !status : false;
                }
 
                if (e.children) {
                    deep(e.children);
                }
            });
        }
 
        deep(config.columns);
    }
 
    // 隐藏列
    function hideColumn(prop: string | string[]) {
        showColumn(prop, false);
    }
 
    // 设置列
    function setColumns(list: ClTable.Column[]) {
        if (list) {
            reBuild(() => {
                config.columns.splice(0, config.columns.length, ...list);
            });
        }
    }
 
    return {
        visible,
        reBuild,
        showColumn,
        hideColumn,
        setColumns
    };
}