zhangwei
2 天以前 9488f2bcb346c32c64f2c4eb5814fece8726b363
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
<template>
    <el-select
            v-model="state.selectedValues"
            :clearable="clearable"
            :multiple="multiple"
            :disabled="disabled"
            :placeholder="placeholder"
            :allow-create="allowCreate"
            :remote-method="remoteMethod"
            :default-first-option="allowCreate"
            @visible-change="remoteMethod()"
            remote-show-suffix
            filterable
            ref="selectRef"
            remote>
        <el-option style="display:none" />
        <el-option v-for="item in state.tableData.items" :key="item[valueField]" :label="item[displayField]" :value="item[valueField]" style="display:none" />
        <el-table :data="state.tableData.items" @row-click="handleChange" :height="props.dropdownHeight" v-loading="state.loading" ref="tableRef" :style="{ width: props.dropdownWidth }" highlight-current-row >
            <template #empty><el-empty :image-size="25" /></template>
            <slot name="columns"></slot>
        </el-table>
    </el-select>
</template>
 
<script lang="ts" setup>
import { reactive, ref, watch } from 'vue';
import {debounce} from "lodash-es";
 
const props = defineProps({
    modelValue: [String, Number, null],
    fetchOptions: {
        type: Function,
        required: true,
    },
    valueField: {
        type: String,
        default: 'id'
    },
    displayField: {
        type: String,
        default: 'name'
    },
    keywordField: {
        type: String,
        default: 'keyword'
    },
    dropdownWidth: {
        type: String,
        default: '100%'
    },
    dropdownHeight: {
        type: String,
        default: '300px'
    },
    placeholder: {
        type: String,
        default: '请输入关键词'
    },
    queryParams: {
        type: Object,
        default: () => {
            return {
                page: 1,
                pageSize: 20
            }
        }
    },
    allowCreate: Boolean,
    disabled: Boolean,
    multiple: Boolean,
    clearable: Boolean,
});
 
const tableRef = ref();
const selectRef = ref();
const emit = defineEmits(['update:modelValue', 'change'])
const state = reactive({
    selectedValues: '' as string | string[],
    tableQuery: {
        [props.keywordField]: '',
        page: 1,
        pageSize: props.queryParams?.pageSize ?? 100
    },
    tableData: {
        items: [],
        total: 0
    },
    loading: false
})
 
// 远程查询方法
const remoteMethod = debounce((query: string = '') => {
    if (query) {
        state.loading = true;
        state.tableQuery[props.keywordField] = query?.trim();
        props.fetchOptions(Object.assign({}, props.queryParams, state.tableQuery)).then((result: any) => {
            state.tableData.items = result.items;
            state.tableData.total = result.total;
            state.loading = false;
        });
    } else {
        state.tableData.items = [];
        state.tableData.total = 0;
    }
}, 500);
 
// 选择值改变事件
const handleChange = (row: any) => {
    if (props.multiple && !state.selectedValues) state.selectedValues = [];
    if (typeof row[props.valueField] === 'string') row[props.valueField] = row[props.valueField]?.trim();
    state.selectedValues = props.multiple ? Array.from(new Set([...state.selectedValues, row[props.valueField]])) : row[props.valueField];
    emit('update:modelValue', state.selectedValues);
    emit('change', state.selectedValues, row);
    if (!props.multiple) selectRef.value?.blur();
    tableRef.value?.setCurrentRow(row);
};
 
watch(
        () => props.modelValue,
        (val: any) => { state.selectedValues = val }
);
</script>
 
<style scoped>
:deep(.el-select-dropdown) {
    .el-scrollbar > .el-scrollbar__bar {
        display: none!important;
    }
}
</style>