yangyin
2024-11-04 063a460cbfbad75b78894f0e22689eb15c049a25
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
<template>
    <el-table
        ref="tableRef"
        border
        :row-key="rowKey"
        :row-class-name="rowClassName"
        :v-bind="dynamicBindProps"
        :v-on="{ expandChange: isTree ? tableExpandChange : undefined }"
        :cell-style="tableCellStyle"
        :header-cell-style="{ textAlign: 'center' }"
        highlight-current-row
        :data="data"
    >
        <el-table-column type="selection" width="55" v-if="hasSelection"> </el-table-column>
        <el-table-column type="index" label="序号" width="60" v-if="hasSerialNo" />
        <el-table-column
            v-for="(item, index) in header"
            :key="item.prop"
            show-overflow-tooltip
            :prop="item.prop"
            :width="item.colWidth"
            :label="item.label"
        >
            <template v-slot="scope">
                <template v-if="item.type === 'image'">
                    <!-- <el-image
                        :style="{ width: `${item.width}px`, height: `${item.height}px` }"
                        :src="scope.row[item.key]"
                        :zoom-rate="1.2"
                        :preview-src-list="[scope.row[item.key]]"
                        preview-teleported
                        fit="cover"
                    /> -->
                </template>
                <el-tag
                    v-else-if="item.type === 'tag'"
                    class="table-tag-space"
                    v-for="(tagItem, index) in scope.row[item.prop]"
                    :key="index"
                    >{{ tagItem }}</el-tag
                >
                <template v-else>
                    {{ scope.row[item.prop] }}
                </template>
            </template>
        </el-table-column>
        <el-table-column label="操作" :width="optColWidth" fixed="right" show-overflow-tooltip>
            <template #default="scope">
                <el-button
                    v-for="(item, index) in rowOperate"
                    :key="index"
                    :icon="item.icon"
                    size="small"
                    text
                    :type="item.buttonType"
                    @click="item.click(scope.row)"
                    >{{ item.text }}</el-button
                >
            </template>
        </el-table-column>
        <template #empty>
            <el-empty description="暂无数据" />
        </template>
    </el-table>
</template>
 
<script setup lang="ts">
import { computed, ref, toRefs } from 'vue';
import { YWTableProps } from '/@/components/table/yw-table';
import { tableCellCenterExceptColumn } from '/@/utils/util';
import { TableInstance } from 'element-plus';
 
const props = defineProps(YWTableProps);
 
const { data, header, rowOperate, hasSelection, hasSerialNo, optColWidth, rowClassName, treeProps, rowKey } = toRefs(props);
const tableRef = ref<TableInstance>(null);
 
/** @description 通过查找是否含有 Children,判断是否为树形表格 */
const isTree = computed(() => {
    const checkChildrenKey = header.value.some((item) => {
        return item.prop === treeProps.value.children;
    });
    return checkChildrenKey;
});
 
const treeTableExpandKeys = ref([]);
 
const dynamicBindProps = computed(() => {
    if (isTree.value) {
        return {
            treeProps: treeProps.value,
            expandRowKeys: treeTableExpandKeys.value,
        };
    } else {
        return undefined;
    }
});
 
const tableCellStyle = computed((): any => {
    const preRowNum = [isTree.value, hasSelection.value].filter((item) => item).length;
    if (preRowNum > 0) {
        return tableCellCenterExceptColumn(preRowNum - 1);
    } else {
        return { textAlign: 'center' };
    }
});
 
const tableExpandChange = (row, expanded) => {
    if (expanded) {
        treeTableExpandKeys.value.push(row.ID);
    } else {
        const idx = treeTableExpandKeys.value.indexOf(row.ID);
        treeTableExpandKeys.value.splice(idx, 1);
    }
};
 
defineExpose({
    tableRef,
});
</script>
<style scoped lang="scss"></style>