gerson
2024-08-04 f052d279131a056cb82c178ae195f463caa72c33
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
<template>
    <div>
        <span v-if="data?.title" class="text-base font-bold flex-center mb-5">{{ data?.title }}</span>
        <div class="w-full" style="height: 70vh" ref="containerRef" v-resize="resizeHandler">
            <el-table
                ref="tableRef"
                border
                :cell-style="{ textAlign: 'center' }"
                :header-cell-style="{ textAlign: 'center' }"
                :data="data?.values"
                highlight-current-row
                class="w-full h-full"
                cellClassName="text-sm"
                headerCellClassName="text-sm"
            >
                <template v-if="data?.cols?.length > 0">
                    <el-table-column
                        v-for="(item, index) in colList"
                        :label="item.title"
                        :width="item.width"
                        :sortable="item.type === 'time'"
                        :key="index"
                        :prop="index + ''"
                        show-overflow-tooltip
                    />
                </template>
            </el-table>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import type { TableInstance } from 'element-plus';
import _ from 'lodash';
import { onMounted, ref, type PropType } from 'vue';
import { debounce, getTextWidth } from '/@/utils/util';
 
const props = defineProps({
    data: {
        type: Object as PropType<any>,
    },
});
 
const colList = ref([]);
const containerRef = ref<HTMLDivElement>(null);
const tableRef = ref<TableInstance>(null);
const measureWidthOffset = 28;
 
const resizeEvent = ({ width, height }) => {
    if (width === 0 || height === 0) {
        return;
    }
    if (props.data?.cols?.length > 0 && props.data?.values?.length > 0) {
        const maxStrList = props.data.cols.map((item) => item.title);
        const maxLenList = props.data.cols.map((item) => item.title.gblen());
        for (const item of props.data.values) {
            item.map((subItem, index) => {
                const subItemLen = subItem?.gblen();
                if (maxLenList[index] < subItemLen) {
                    maxLenList[index] = subItemLen;
                    maxStrList[index] = subItem;
                }
            });
        }
        // 总宽
        let sumWidth = 0;
        let maxWidthList = maxStrList.map((item, index) => {
            const width =
                getTextWidth(item, {
                    size: '0.875rem',
                }) + measureWidthOffset;
            sumWidth += width;
            return {
                index,
                maxWidth: width,
            };
        });
        if (sumWidth <= width) {
            maxWidthList = maxWidthList.map((item) => ({
                ...item,
                width: (item.maxWidth / sumWidth) * width,
            }));
            // 先满足小的,剩余空间按比例均分
        } else {
            let curWidth = 0;
            const sortedWidthList = _.sortBy(maxWidthList, 'maxWidth');
            let restWidth = width;
            let notFitStartIndex = 1;
            for (let index = 0; index < sortedWidthList.length; index++) {
                const item = sortedWidthList[index];
                curWidth += item.maxWidth;
                if (curWidth < width || index === 0) {
                    maxWidthList[item.index].width = item.maxWidth;
                    continue;
                }
                restWidth = width - (curWidth - item.maxWidth);
                notFitStartIndex = index;
                break;
            }
            let sumRestMaxWidth = 0;
            const notFitIndexList = sortedWidthList.slice(notFitStartIndex).map((item) => {
                sumRestMaxWidth += item.maxWidth;
                return item.index;
            });
 
            // 平均分配剩余空间
            for (const item of notFitIndexList) {
                maxWidthList[item].width = restWidth * (maxWidthList[item].maxWidth / sumRestMaxWidth);
                if (maxWidthList[item].maxWidth <= restWidth) {
                    maxWidthList[item].width = restWidth;
                }
 
                // maxWidthList[item].width = undefined;
            }
        }
        colList.value = props.data.cols.map((item, index) => ({
            ...item,
            width: maxWidthList[index].width,
        }));
    } else {
        colList.value = [];
    }
    tableRef.value.doLayout();
};
 
const resizeHandler = debounce(resizeEvent);
 
onMounted(() => {
    resizeEvent({
        width: containerRef.value.clientWidth,
        height: 0.7 * document.body.clientHeight,
    });
});
</script>
<style scoped lang="scss"></style>