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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { defineComponent, h } from "vue";
import {
    useRow,
    useHeight,
    useRender,
    useSort,
    useData,
    useSelection,
    useOp,
    useTable
} from "./helper";
import { useCore, useProxy, useElApi, useConfig } from "../../hooks";
import { usePlugins } from "./helper/plugins";
 
export default defineComponent({
    name: "cl-table",
 
    props: {
        // 列配置
        columns: {
            type: Array,
            default: () => []
        },
        // 是否自动计算高度
        autoHeight: {
            type: Boolean,
            default: null
        },
        // 固定高度
        height: null,
        // 右键菜单
        contextMenu: {
            type: [Array, Boolean],
            default: null
        },
        // 默认排序
        defaultSort: Object,
        // 排序后是否刷新
        sortRefresh: {
            type: Boolean,
            default: true
        },
        // 空数据显示文案
        emptyText: String,
        // 当前行的 key
        rowKey: {
            type: String,
            default: "id"
        }
    },
 
    emits: ["selection-change", "sort-change"],
 
    setup(props, { emit, expose }) {
        const { crud } = useCore();
        const { style } = useConfig();
        const { Table, config } = useTable(props);
        const plugin = usePlugins();
 
        // 排序
        const Sort = useSort({ config, emit, Table });
 
        // 行
        const Row = useRow({
            config,
            Table,
            Sort
        });
 
        // 高度
        const Height = useHeight({ config, Table });
 
        // 数据
        const Data = useData({ config, Table });
 
        // 多选
        const Selection = useSelection({ emit });
 
        // 操作
        const Op = useOp({ config });
 
        // 方法
        const ElTableApi = useElApi(
            [
                "clearSelection",
                "getSelectionRows",
                "toggleRowSelection",
                "toggleAllSelection",
                "toggleRowExpansion",
                "setCurrentRow",
                "clearSort",
                "clearFilter",
                "doLayout",
                "sort",
                "scrollTo",
                "setScrollTop",
                "setScrollLeft",
                "updateKeyChildren"
            ],
            Table
        );
 
        const ctx = {
            Table,
            config,
            columns: config.columns,
            ...Selection,
            ...Data,
            ...Sort,
            ...Row,
            ...Height,
            ...Op,
            ...ElTableApi
        };
 
        useProxy(ctx);
        expose(ctx);
        plugin.create(config.plugins);
 
        return () => {
            const { renderColumn, renderAppend, renderEmpty } = useRender();
 
            return (
                ctx.visible.value &&
                h(
                    <el-table class="cl-table" ref={Table} v-loading={crud.loading} />,
                    {
                        ...config.on,
                        ...config.props,
 
                        // config
                        maxHeight: config.autoHeight ? ctx.maxHeight.value : null,
                        height: config.autoHeight ? config.height : null,
                        rowKey: config.rowKey,
 
                        // ctx
                        defaultSort: ctx.defaultSort,
                        data: ctx.data.value,
                        onRowContextmenu: ctx.onRowContextMenu,
                        onSelectionChange: ctx.onSelectionChange,
                        onSortChange: ctx.onSortChange,
 
                        // style
                        size: style.size,
                        border: style.table.border,
                        highlightCurrentRow: style.table.highlightCurrentRow,
                        resizable: style.table.resizable,
                        stripe: style.table.stripe,
                    },
                    {
                        default() {
                            return renderColumn(ctx.columns);
                        },
                        empty() {
                            return renderEmpty(config.emptyText || crud.dict.label.empty);
                        },
                        append() {
                            return renderAppend();
                        }
                    }
                )
            );
        };
    }
});