wangrong
2026-09-29 e2d1d78bc72459a0ac2e2e337bbca7c8a1e41e17
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { useCrud, useForm } from '@cool-vue/crud';
import { cloneDeep, isEmpty, isFunction, isString } from 'lodash-es';
import {
    computed,
    defineComponent,
    onActivated,
    onMounted,
    type PropType,
    ref,
    type Ref,
    toValue,
    useModel
} from 'vue';
import { deepTree, parsePx } from '/@/cool/utils';
import { useI18n } from 'vue-i18n';
import { useCool } from '/@/cool';
import { CrudProps } from '../../comm';
 
export default defineComponent({
    name: 'cl-select',
 
    props: {
        ...CrudProps,
        modelValue: [String, Number, Array],
        options: {
            type: [Array, Object] as PropType<any[] | Ref<any[]>>,
            default: () => []
        },
        prop: String,
        labelKey: {
            type: String,
            default: 'label'
        },
        valueKey: {
            type: String,
            default: 'value'
        },
        width: [String, Number],
 
        // 是否树形
        tree: Boolean,
        // 是否返回选中层级下的所有值
        allLevelsId: Boolean,
        // 是否父子不互相关联
        checkStrictly: Boolean,
        // 默认展开所有
        defaultExpandAll: Boolean,
        // 顶级标签
        topLabel: String,
        // 是否选择当前表格数据,如果是,则不能选择自身及其下级
        current: Boolean,
 
        // 请求接口,如果是字符串,则使用 crud 下的方法
        api: [String, Function],
 
        // 值变化刷新
        refreshOnChange: {
            type: Boolean,
            default: true
        },
        // 是否在激活时刷新
        refreshOnActivated: {
            type: Boolean,
            default: true
        }
    },
 
    emits: ['update:modelValue', 'change'],
 
    setup(props, { emit, expose }) {
        const { refs, setRefs, route } = useCool();
        const { t } = useI18n();
 
        // cl-crud
        const Crud = useCrud();
 
        // cl-form
        const Form = useForm();
 
        // 是否用于搜索
        const isSearch = computed(() => !Form.value || Form.value?.name === 'search');
 
        // 选项
        const options = ref<Dict.Item[]>();
 
        // 选中值
        const value = useModel(props, 'modelValue');
 
        // 列表
        const list = computed(() => {
            let data: any[] = [];
 
            if (props.current) {
                data = cloneDeep(toValue(Crud.value?.['cl-table']?.data));
 
                // 禁用自身及其下级
                function deep(d: any, f: boolean) {
                    if (d.id && d.id == Form.value?.getForm('id')) {
                        f = true;
                    }
 
                    if (f) {
                        d.disabled = true;
                    }
 
                    if (d.children) {
                        d.children.forEach((e: any) => {
                            deep(e, f);
                        });
                    }
                }
 
                deep({ children: data }, false);
            } else {
                data = toValue(options.value || props.options) || [];
            }
 
            // 如果存在顶级标签
            if (props.topLabel) {
                return [
                    {
                        [props.labelKey]: props.topLabel,
                        [props.valueKey]: 0,
                        children: data
                    }
                ];
            }
 
            return data;
        });
 
        // 获取值
        function getValue(val: any): any | any[] {
            if (props.allLevelsId) {
                const ids: any[] = [];
 
                // 获取所有的值
                const deep = (arr: Dict.Item[], f: boolean) => {
                    arr.forEach(e => {
                        const f2 = e[props.valueKey] == val;
 
                        if (f || f2) {
                            ids.push(e[props.valueKey]);
                        }
 
                        if (e.children) {
                            deep(e.children, f || f2);
                        }
                    });
                };
 
                deep(list.value, false);
 
                return isEmpty(ids) ? undefined : ids;
            } else {
                return val === '' ? undefined : val;
            }
        }
 
        // 值改变
        function onChange(val: any) {
            const v = getValue(val);
 
            emit('update:modelValue', v);
            emit('change', v);
 
            if (isSearch.value) {
                if (props.prop && props.refreshOnChange) {
                    Crud.value?.refresh({ page: 1, [props.prop]: v });
                }
            }
        }
 
        // 聚焦
        function focus() {
            refs.select?.focus();
        }
 
        // 获取选项数据
        function refresh() {
            let req: Promise<any> | null = null;
 
            if (isString(props.api)) {
                req = Crud.value?.service[props.api]();
            }
 
            if (isFunction(props.api)) {
                req = props.api();
            }
 
            if (req) {
                req.then(res => {
                    options.value = deepTree(res);
                });
            }
        }
 
        onMounted(() => {
            if (props.api) {
                refresh();
 
                onActivated(() => {
                    if (props.refreshOnActivated) {
                        if (!Form.value || Form.value?.name == 'search') {
                            refresh();
                        }
                    }
                });
            }
        });
 
        expose({
            focus
        });
 
        return () => {
            // 样式
            const style = {
                width: parsePx(props.width!)
            };
 
            // 占位符
            const placeholder = isSearch.value ? t('全部') : t('请选择');
 
            // 树形下拉框
            const TreeSelect = (
                <el-tree-select
                    v-model={value.value}
                    clearable
                    filterable
                    placeholder={placeholder}
                    disabled={props.disabled}
                    data={list.value}
                    checkStrictly={props.allLevelsId || props.checkStrictly}
                    defaultExpandAll={props.defaultExpandAll}
                    props={{
                        label: props.labelKey,
                        value: props.valueKey
                    }}
                    style={style}
                    onChange={onChange}
                    ref={setRefs('select')}
                />
            );
 
            // 普通下拉框
            const Select = (
                <el-select
                    v-model={value.value}
                    clearable
                    filterable
                    disabled={props.disabled}
                    placeholder={placeholder}
                    style={style}
                    onChange={onChange}
                    ref={setRefs('select')}
                >
                    {list.value?.map(e => {
                        return isString(e) ? (
                            <el-option label={e} value={e} />
                        ) : (
                            <el-option
                                disabled={e.disabled}
                                label={e[props.labelKey]}
                                value={e[props.valueKey]}
                            />
                        );
                    })}
                </el-select>
            );
 
            return props.tree ? TreeSelect : Select;
        };
    }
});