/** * 选择产地 */ Vue.component('select-goods-yieldly', { template: ` `, props: { 'yieldlyId': { type: String, default: '', },//选择的产地ID 'yieldlyName': { type: String, default: '', },//选择的产地名称 'isDisabled': { type: Boolean, default: false, },//是否禁止修改 'placeholder': { type: String, default: '商品产地(可直接新增)' }, 'size': { type: String, default: 'small', } }, data() { return { yieldly_id: '',//已选中产地ID dataList: [],// 数据 }; }, methods: { //切换产地 changeYieldly(val) { this.$nextTick(() => { if (this.$refs.yieldlyLabel.createdLabel) { this.$confirm("确认添加到产地库吗?", "温馨提示", { confirmButtonText: '添加', cancelButtonText: '取消', type: 'info' }).then(() => { this.sendRequest('Retail/Goods/addGoodsYieldly', { data: { name: val }, success: (res) => { if (res.status == 1) { this.$message.success(res.message); let obj = { id: res.data.id, name: val, }; this.dataList.unshift(obj); this.$refs.yieldlyLabel.createdLabel = null; this.$nextTick(() => { this.yieldly_id = res.data.id; this.callback(); }); } } }) }).catch(() => { this.$refs.yieldlyLabel.createdLabel = null; this.yieldly_id = ''; this.callback(); }) } else { this.callback(); } }) }, callback() { this.$emit('update:yieldlyId', this.yieldly_id); this.$emit('change', this.yieldly_id); }, //获取产地列表 getYieldlyList() { this.dataList = []; this.sendRequest('Retail/Goods/getGoodsYieldlyList', { success: (res) => { if (res.status == 1) { this.dataList = res.data; this.$nextTick(() => { if (this.yieldlyName) { let flag = false; for (let i = 0; i < this.dataList.length; i++) { if (this.dataList[i].name == this.yieldlyName) { this.yieldly_id = this.dataList[i].id; flag = true; break; } } if (!flag) { this.$refs.yieldlyLabel.createdLabel = this.yieldlyName; this.changeYieldly(this.yieldlyName); } } else { this.$nextTick(() => { this.$refs.yieldlyLabel.createdLabel = null; }) } }) } }, }) }, }, created() { this.yieldly_id = this.yieldlyId; this.getYieldlyList(); }, watch: { yieldlyId(n) { this.yieldly_id = n; }, yieldlyName(n) { if (n != '' && n) { this.getYieldlyList(); } } } });