index.vue 3.25 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('请输入商品关键词')"
dragondean@qq.com's avatar
dragondean@qq.com committed
9
        :disabled="disabled"
10
        :remote-method="remoteMethod"
dragondean@qq.com's avatar
dragondean@qq.com committed
11 12
        @focus="onFocus"
        @clear="onClear"
13 14 15 16
        :loading="loading">
        <el-option
        v-for="(item, index) in list"
        :key="item.id"
17
        :label="item['title' + lang] + (item.auditStatus === 2 ? '' : `(${lang === 'Zh' ? '待确定' : 'To be determined'})`)"
18
        :value="index">
19 20 21 22
          <span style="float: left">{{ item['title' + lang] }}</span>
          <span style="float: right; color: #c76e6e; font-size: 13px">
            {{ item.auditStatus === 2 ? '' : (lang === 'Zh' ? '待确定' : 'To be determined') }}
          </span>
23 24 25 26 27 28 29 30 31
        </el-option>
    </el-select>
</template>
<script>
import {getProduct, getProductPage} from '@/api/ecw/product'

export default {
    props:{
        productType: [String, Number],
dragondean@qq.com's avatar
dragondean@qq.com committed
32
        value: [String, Number],
dragondean@qq.com's avatar
dragondean@qq.com committed
33 34 35 36
        disabled: Boolean,
        lang: {
            type: String,
            default: 'Zh'
37 38 39 40 41
        },
        // 是否显示待确认商品,不传默认不显示
        determined: {
          type: Boolean,
          default: false
42 43 44 45 46
        },
        // 是否显示已有商品,不传默认
        status: {
          type: [String, Number],
          default: null
dragondean@qq.com's avatar
dragondean@qq.com committed
47
        }
48 49 50
    },
    data(){
        return {
dragondean@qq.com's avatar
dragondean@qq.com committed
51
            index: null,
52 53 54 55 56
            list:[],
            loading: false
        }
    },
    watch:{
dragondean@qq.com's avatar
dragondean@qq.com committed
57
        index(val, oldVal){
dragondean@qq.com's avatar
dragondean@qq.com committed
58
            let productId = val !== '' && val !== null ? this.list[val].id : null
dragondean@qq.com's avatar
dragondean@qq.com committed
59
            // console.log('index val', val, oldVal, productId)
dragondean@qq.com's avatar
dragondean@qq.com committed
60
            this.$emit('input', productId)
dragondean@qq.com's avatar
dragondean@qq.com committed
61
            this.$emit('change', val !== '' && val !== null ? this.list[val] : null)
62 63
        },
        value(val){
dragondean@qq.com's avatar
dragondean@qq.com committed
64
            // console.log('初始化内容', val)
65 66 67 68
            this.init()
        }
    },
    created(){
dragondean@qq.com's avatar
dragondean@qq.com committed
69
        // console.log('created', this.value, this.lang)
70 71 72 73
        this.init()
    },
    methods:{
        init(){
74 75
            if(!this.value){
                this.index = null
76
                return
77
            }
78 79 80 81
            let index = this.list.findIndex(item => item.id == this.value)
            if(index < 0){
                getProduct(this.value).then(res => {
                    this.list.unshift(res.data)
82 83 84
                    this.$nextTick(() => {
                        this.index = 0
                    })
85
                })
86
            }else this.index = index
87
        },
dragondean@qq.com's avatar
dragondean@qq.com committed
88
        onFocus(){
89
            this.$emit('focus')
dragondean@qq.com's avatar
dragondean@qq.com committed
90 91 92 93 94 95 96
            if(!this.list.length){
                this.remoteMethod()
            }
        },
        onClear(){
            this.list = []
        },
97
        remoteMethod(keyword){
98
            let params = {
99
                pageSize: keyword ? 100000 : 10,
100 101
                filter: !this.determined,
                status:this.status
102
            }
103
            if(this.productType){
104
                params.typeId = this.productType
105 106 107 108 109 110 111 112 113
            }
            params.titleZh = keyword
            this.loading = true
            getProductPage(params)
                .then(res => this.list = res.data.list)
                .finally(() => this.loading = false)
        }
    }
}
114
</script>