RequestSelect.vue 1.39 KB
Newer Older
陈浩玮's avatar
yonghu  
陈浩玮 committed
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
<template>
    <a-select
        :value="value"
        style="width: 100%"
        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
        placeholder="Please select"
        allow-clear
        :options="data"
        @change="onChange"
        v-bind="$attrs"
    />
</template>

<script>
import { formatObj } from '@/utils';

export default {
    model: {
        prop: 'value',
        event: 'change',
    },
    props: {
        value: [String, Number, Array],
        request: Function,
        formatData: { type: Object, default: undefined },
        getName: Function,
陈浩玮's avatar
陈浩玮 committed
27
        param: { type: Object, default: () => ({}) },
水落(YangLei)'s avatar
水落(YangLei) committed
28 29
        labelFiled: String, // 和 formatData 一样 传一种就行了
        valueFiled: String,
陈浩玮's avatar
yonghu  
陈浩玮 committed
30 31 32 33 34 35 36 37
    },
    data() {
        return {
            data: [],
        };
    },
    async mounted() {
        if (this.request) {
陈浩玮's avatar
陈浩玮 committed
38
            const newArr = await this.request(this.param);
陈浩玮's avatar
yonghu  
陈浩玮 committed
39
            this.data = this.formatData ? await formatObj(newArr, this.formatData) : newArr;
水落(YangLei)'s avatar
水落(YangLei) committed
40 41 42
            this.data = this.labelFiled
                ? formatObj(newArr, { label: this.labelFiled, value: this.valueFiled, key: this.valueFiled })
                : this.data;
陈浩玮's avatar
yonghu  
陈浩玮 committed
43 44 45 46 47 48 49 50 51 52
        }
    },
    methods: {
        onChange(val, opt) {
            this.$emit('change', val);
            this.getName && this.getName(opt?.componentOptions?.children[0]?.text || '');
        },
    },
};
</script>