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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
    <div class="cl-column-custom__wrap">
        <el-button @click="open">{{ $t('自定义列') }}</el-button>
 
        <cl-dialog v-model="visible" :title="$t('自定义列')">
            <div class="cl-column-custom__dialog">
                <div class="left">
                    <draggable v-model="list" item-key="prop">
                        <template #item="{ element: item }">
                            <el-checkbox v-model="item.checked" border>{{
                                item.label
                            }}</el-checkbox>
                        </template>
                    </draggable>
                </div>
 
                <div class="right"></div>
            </div>
 
            <template #footer>
                <el-button @click="close">{{ $t('取消') }}</el-button>
                <el-button type="danger" @click="reset">{{ $t('重置') }}</el-button>
                <el-button type="success" @click="save">{{ $t('保存') }}</el-button>
            </template>
        </cl-dialog>
    </div>
</template>
 
<script lang="ts" setup>
defineOptions({
    name: 'cl-column-custom'
});
 
import { ref, type PropType, nextTick, watch, computed } from 'vue';
import Draggable from 'vuedraggable/src/vuedraggable';
import { has, isBoolean, orderBy } from 'lodash-es';
import { storage } from '/@/cool';
 
const props = defineProps({
    name: String,
    columns: {
        type: Array as PropType<any[]>,
        required: true,
        default: () => []
    }
});
 
const emit = defineEmits(['update:modelValue', 'change']);
 
// 是否可见
const visible = ref(false);
 
// 名称
const name = `column-custom__${props.name || location.pathname}`;
 
// 列数据
const list = ref<{ label: string; prop: any; checked?: boolean; orderNum?: number }[]>([]);
 
// 列配置
const columns = computed(() => {
    return props.columns.filter(e => !e.type && e.prop);
});
 
// 改变列
function change() {
    nextTick(() => {
        columns.value.forEach(e => {
            e.hidden = !list.value.find(a => a.prop == e.prop)?.checked;
            e.orderNum = list.value.findIndex(a => a.prop == e.prop);
        });
    });
}
 
// 保存
function save() {
    storage.set(name, list.value);
    change();
    close();
}
 
// 重置
function reset() {
    columns.value.forEach(e => {
        if (e.orderNum !== undefined) {
            e.hidden = false;
            e.orderNum = 0;
        }
    });
 
    storage.remove(name);
    refresh();
    close();
}
 
// 打开
function open() {
    visible.value = true;
    refresh();
}
 
// 关闭
function close() {
    visible.value = false;
}
 
// 排序
function sort(list: any[]) {
    return orderBy(list, 'orderNum', 'asc');
}
 
// 刷新
function refresh() {
    if (!props.columns) {
        return false;
    }
 
    const selection: any[] = storage.get(name);
 
    list.value = sort(
        columns.value.map(e => {
            let checked = true;
            let orderNum = e.orderNum || 0;
 
            if (isBoolean(e.hidden)) {
                checked = !e.hidden;
            }
 
            if (selection) {
                checked = selection.find(a => a.prop == e.prop)?.checked;
                orderNum = selection.findIndex(a => a.prop == e.prop);
            }
 
            return {
                label: e.label,
                prop: e.prop,
                checked,
                orderNum
            };
        })
    );
 
    change();
}
 
// 合计
function summary(subData: { [key: string]: any }) {
    return sort(columns.value.filter(e => !e.hidden)).map(e => {
        if (has(subData, e.prop)) {
            return subData[e.prop];
        } else {
            return '';
        }
    });
}
 
watch(
    () => props.columns,
    () => {
        refresh();
    }
);
 
defineExpose({
    summary
});
</script>
 
<style lang="scss" scoped>
.cl-column-custom {
    &__wrap {
        margin-left: 10px;
    }
 
    &__dialog {
        .left {
            .el-checkbox {
                margin: 5px 10px 5px 0;
            }
        }
 
        .right {
            border-left: 1px solid #eee;
        }
    }
}
</style>