index.vue 1.67 KB
Newer Older
1 2 3 4 5 6 7
<template>
    <el-select
        v-model="index"
        filterable
        clearable
        remote
        reserve-keyword
dragondean@qq.com's avatar
dragondean@qq.com committed
8
        :placeholder="$t('请输入关键词')"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
        :remote-method="remoteMethod"
        :loading="loading">
        <el-option
        v-for="(item, index) in list"
        :key="item.id"
        :label="`${item.name}(${item.number})`"
        :value="index">
        </el-option>
    </el-select>
</template>
<script>
import {getCustomerSelect, getCustomer} from '@/api/ecw/customer'

export default {
    props:{
        productType: [String, Number],
        value: [String, Number]
    },
    data(){
        return {
29
            index: null,
30 31 32 33 34 35
            list:[],
            loading: false
        }
    },
    watch:{
        index(val){
我在何方's avatar
我在何方 committed
36 37
            this.$emit('input', (val!==''&&val !== null) ? this.list[val].id: null)
            this.$emit('change', (val!==''&&val !== null) ? this.list[val]: null)
38 39 40 41 42 43 44 45 46 47
        },
        value(val){
            this.init()
        }
    },
    created(){
        this.init()
    },
    methods:{
        init(){
dragondean@qq.com's avatar
dragondean@qq.com committed
48
            if(!this.value) return
49 50 51 52 53 54 55 56 57 58 59 60 61
            let index = this.list.findIndex(item => item.id == this.value)
            if(index < 0){
                getCustomer(this.value).then(res => {
                    this.list.unshift(res.data)
                    this.index = 0
                })
            }
        },
        remoteMethod(keyword){
            let params = {}
            params.searchKey = keyword
            this.loading = true
            getCustomerSelect(params)
62
                .then(res => this.list = res.data.list)
63 64 65 66
                .finally(() => this.loading = false)
        }
    }
}
我在何方's avatar
我在何方 committed
67
</script>