index.vue 3.78 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
47 48 49 50 51
        },
        // 为true时阻挡第一次$emit('change'),用于阻止入仓修改初始化时时覆盖了已修改的入仓属性
        protectOnce: {
            type: Boolean,
            default: false
dragondean@qq.com's avatar
dragondean@qq.com committed
52
        }
53 54 55
    },
    data(){
        return {
dragondean@qq.com's avatar
dragondean@qq.com committed
56
            index: null,
57
            list:[],
58 59 60
            loading: false,
            // 是否阻止过$emit('change')
            hasProtectOnce: false
61 62 63
        }
    },
    watch:{
dragondean@qq.com's avatar
dragondean@qq.com committed
64
        index(val, oldVal){
dragondean@qq.com's avatar
dragondean@qq.com committed
65
            let productId = val !== '' && val !== null ? this.list[val].id : null
dragondean@qq.com's avatar
dragondean@qq.com committed
66
            // console.log('index val', val, oldVal, productId)
dragondean@qq.com's avatar
dragondean@qq.com committed
67
            this.$emit('input', productId)
68 69 70 71 72 73
            if (this.protectOnce && !this.hasProtectOnce) {
              this.hasProtectOnce = true
            } else {
                this.$emit('change', val !== '' && val !== null ? this.list[val] : null)
            }

74 75
        },
        value(val){
dragondean@qq.com's avatar
dragondean@qq.com committed
76
            // console.log('初始化内容', val)
77
            this.init()
78 79 80 81
        },
        // 类型变了,清空内容
        productType(){
            this.list = []
82 83 84
        }
    },
    created(){
dragondean@qq.com's avatar
dragondean@qq.com committed
85
        // console.log('created', this.value, this.lang)
86 87 88 89
        this.init()
    },
    methods:{
        init(){
90 91
            if(!this.value){
                this.index = null
92
                return
93
            }
94 95 96 97
            let index = this.list.findIndex(item => item.id == this.value)
            if(index < 0){
                getProduct(this.value).then(res => {
                    this.list.unshift(res.data)
98 99 100
                    this.$nextTick(() => {
                        this.index = 0
                    })
101
                })
102
            }else this.index = index
103
        },
dragondean@qq.com's avatar
dragondean@qq.com committed
104
        onFocus(){
105
            this.$emit('focus')
dragondean@qq.com's avatar
dragondean@qq.com committed
106 107 108 109 110 111 112
            if(!this.list.length){
                this.remoteMethod()
            }
        },
        onClear(){
            this.list = []
        },
113
        remoteMethod(keyword){
114
            let params = {
115
                pageSize: keyword ? 100000 : 10,
116 117
                filter: !this.determined,
                status:this.status
118
            }
119
            if(this.productType){
120
                params.typeId = this.productType
121 122 123 124 125 126 127 128 129
            }
            params.titleZh = keyword
            this.loading = true
            getProductPage(params)
                .then(res => this.list = res.data.list)
                .finally(() => this.loading = false)
        }
    }
}
130
</script>