search_select.vue 1.19 KB
Newer Older
水落(YangLei)'s avatar
水落(YangLei) 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 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
<template>
    <a-select
        show-search
        placeholder="input search text"
        :default-active-first-option="false"
        :show-arrow="false"
        :filter-option="false"
        @search="handleSearch"
        allowClear
        :options="options"
        :mode="mode"
        v-on="$listeners"
    />
</template>

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

export default {
    props: {
        url: String,
        searchField: String,
        formatData: Object,
        mode: String,
    },
    model: {
        prop: 'value',
        event: 'change',
    },
    data() {
        return {
            options: [],
        };
    },
    mounted() {
        this.getData('');
    },
    methods: {
        async getData(searchString) {
            this.options = formatObj(
                await getReq(this.url, { [this.searchField]: searchString }),
                this.formatData,
            );
        },
        handleSearch(value) {
            if (this.timeOut) clearTimeout(this.timeOut);
            this.timeOut = setTimeout(() => {
                if (value) this.getData(value);
            }, 300);
        },
    },
};
</script>