no message
							parent
							
								
									df31755799
								
							
						
					
					
						commit
						fdbe124e02
					
				| 
						 | 
					@ -16,7 +16,7 @@
 | 
				
			||||||
      v-for="item in items"
 | 
					      v-for="item in items"
 | 
				
			||||||
      :key="item.id"
 | 
					      :key="item.id"
 | 
				
			||||||
      :label="item.code"
 | 
					      :label="item.code"
 | 
				
			||||||
      :value="returnType === 'object' ? item : item[returnValueKey]"
 | 
					      :value="getOptionValue(item)"
 | 
				
			||||||
    >
 | 
					    >
 | 
				
			||||||
      <span>{{ item.code }}</span>
 | 
					      <span>{{ item.code }}</span>
 | 
				
			||||||
      <span>{{ item.name }}</span>
 | 
					      <span>{{ item.name }}</span>
 | 
				
			||||||
| 
						 | 
					@ -33,7 +33,7 @@ export default {
 | 
				
			||||||
  props: {
 | 
					  props: {
 | 
				
			||||||
    value: {
 | 
					    value: {
 | 
				
			||||||
      type: [String, Number, Boolean, Array, Object],
 | 
					      type: [String, Number, Boolean, Array, Object],
 | 
				
			||||||
      default: ''
 | 
					      default: () => null
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    width: {
 | 
					    width: {
 | 
				
			||||||
      type: String,
 | 
					      type: String,
 | 
				
			||||||
| 
						 | 
					@ -43,18 +43,12 @@ export default {
 | 
				
			||||||
      type: String,
 | 
					      type: String,
 | 
				
			||||||
      default: '请选择'
 | 
					      default: '请选择'
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    disabled: {
 | 
					    disabled: Boolean,
 | 
				
			||||||
      type: Boolean,
 | 
					 | 
				
			||||||
      default: false
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    clearable: {
 | 
					    clearable: {
 | 
				
			||||||
      type: Boolean,
 | 
					      type: Boolean,
 | 
				
			||||||
      default: true
 | 
					      default: true
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    multiple: {
 | 
					    multiple: Boolean,
 | 
				
			||||||
      type: Boolean,
 | 
					 | 
				
			||||||
      default: false
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    filterable: {
 | 
					    filterable: {
 | 
				
			||||||
      type: Boolean,
 | 
					      type: Boolean,
 | 
				
			||||||
      default: true
 | 
					      default: true
 | 
				
			||||||
| 
						 | 
					@ -71,26 +65,24 @@ export default {
 | 
				
			||||||
      type: Boolean,
 | 
					      type: Boolean,
 | 
				
			||||||
      default: true
 | 
					      default: true
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    // 新增:返回类型控制
 | 
					 | 
				
			||||||
    returnType: {
 | 
					    returnType: {
 | 
				
			||||||
      type: String,
 | 
					      type: String,
 | 
				
			||||||
      default: 'object', // 'object' | 'value'
 | 
					      default: 'object',
 | 
				
			||||||
      validator: function(value) {
 | 
					      validator: value => ['object', 'value'].includes(value)
 | 
				
			||||||
        return ['object', 'value'].includes(value)
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    // 新增:当returnType为'value'时,指定返回哪个字段的值
 | 
					 | 
				
			||||||
    returnValueKey: {
 | 
					    returnValueKey: {
 | 
				
			||||||
      type: String,
 | 
					      type: String,
 | 
				
			||||||
      default: 'id'
 | 
					      default: 'id'
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  data() {
 | 
					  data() {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      items: [],
 | 
					      items: [],
 | 
				
			||||||
      loading: false
 | 
					      loading: false
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  computed: {
 | 
					  computed: {
 | 
				
			||||||
    selectedValue: {
 | 
					    selectedValue: {
 | 
				
			||||||
      get() {
 | 
					      get() {
 | 
				
			||||||
| 
						 | 
					@ -101,77 +93,38 @@ export default {
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  watch: {
 | 
					
 | 
				
			||||||
    // 监听returnType变化,重新初始化选中值
 | 
					 | 
				
			||||||
    returnType: {
 | 
					 | 
				
			||||||
      handler() {
 | 
					 | 
				
			||||||
        this.handleReturnTypeChange()
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      immediate: true
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  },
 | 
					 | 
				
			||||||
  created() {
 | 
					  created() {
 | 
				
			||||||
    this.initData()
 | 
					    this.initData()
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  methods: {
 | 
					  methods: {
 | 
				
			||||||
    async initData() {
 | 
					    async initData() {
 | 
				
			||||||
      this.loading = true
 | 
					      this.loading = true
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        const res = await curdArea.queryAreaList({ enabled: this.isEnabled, bexb: this.bexb })
 | 
					        const res = await curdArea.queryAreaList({
 | 
				
			||||||
 | 
					          enabled: this.isEnabled,
 | 
				
			||||||
 | 
					          bexb: this.bexb
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
        this.items = res || []
 | 
					        this.items = res || []
 | 
				
			||||||
      } catch (error) {
 | 
					      } catch (error) {
 | 
				
			||||||
        this.crud.notify('获取数据失败', CRUD.NOTIFICATION_TYPE.ERROR)
 | 
					        this.crud.notify('获取数据失败', CRUD.NOTIFICATION_TYPE.ERROR)
 | 
				
			||||||
        this.items = []
 | 
					        this.items = []
 | 
				
			||||||
        this.loading = false
 | 
					 | 
				
			||||||
      } finally {
 | 
					      } finally {
 | 
				
			||||||
        this.loading = false
 | 
					        this.loading = false
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getOptionValue(item) {
 | 
				
			||||||
 | 
					      return this.returnType === 'object' ? item : item[this.returnValueKey]
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    handleChange(val) {
 | 
					    handleChange(val) {
 | 
				
			||||||
      let emitValue = val
 | 
					      this.$emit('change', val)
 | 
				
			||||||
 | 
					 | 
				
			||||||
      // 根据返回类型处理数据
 | 
					 | 
				
			||||||
      if (this.returnType === 'value' && val) {
 | 
					 | 
				
			||||||
        if (this.multiple) {
 | 
					 | 
				
			||||||
          // 多选情况
 | 
					 | 
				
			||||||
          emitValue = Array.isArray(val) ? val.map(item =>
 | 
					 | 
				
			||||||
            typeof item === 'object' ? item[this.returnValueKey] : item
 | 
					 | 
				
			||||||
          ) : []
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          // 单选情况
 | 
					 | 
				
			||||||
          emitValue = typeof val === 'object' ? val[this.returnValueKey] : val
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      this.$emit('change', emitValue)
 | 
					 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    handleClear() {
 | 
					    handleClear() {
 | 
				
			||||||
      this.$emit('clear')
 | 
					      this.$emit('clear')
 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // 处理返回类型变化
 | 
					 | 
				
			||||||
    handleReturnTypeChange() {
 | 
					 | 
				
			||||||
      if (this.returnType === 'value') {
 | 
					 | 
				
			||||||
        // 从对象转换为值
 | 
					 | 
				
			||||||
        if (this.multiple) {
 | 
					 | 
				
			||||||
          const values = Array.isArray(this.value) ? this.value.map(item => typeof item === 'object' ? item[this.returnValueKey] : item) : []
 | 
					 | 
				
			||||||
          this.$emit('input', values)
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          const value = typeof this.value === 'object' ? this.value[this.returnValueKey] : this.value
 | 
					 | 
				
			||||||
          this.$emit('input', value)
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        // 默认对象
 | 
					 | 
				
			||||||
        if (this.multiple) {
 | 
					 | 
				
			||||||
          const objects = Array.isArray(this.value) ? this.value.map(val => this.items.find(item => item[this.returnValueKey] === val) || val) : []
 | 
					 | 
				
			||||||
          this.$emit('input', objects)
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          const object = this.items.find(item => item[this.returnValueKey] === this.value) || this.value
 | 
					 | 
				
			||||||
          this.$emit('input', object)
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue