笔记-antd 基于autocomplete可输入可下拉选择

项目中需要使用可下拉又可手动输入的控件,于是基于autocomplete自定了一个,下拉时获取name和id给接口,手动输入时只传name

一、创建SelectAutoComplete.vue

<template>
  <div>
    <a-auto-complete
      v-model="record[propName]"
      :placeholder="holder"
      :allowClear="true"
      option-label-prop="title"
      :disabled="disabled"
      @select="(value,title) => handleSelect(value, record, title)"
      @change="e => handleChange(e, record)"
    >
      <template slot="dataSource">
        <a-select-option v-for="item in selectSource" :key="item.value" :value="item.value" :title="item.label">
          {{ item.label }}
        </a-select-option>
      </template>
    </a-auto-complete>
  </div>
</template>

<script>
export default {
  name: 'SelectAutoComplete',
  props: {
    selectSource: {
      type: Array,
      required: true,
      default: () => []
    },
    disabled: {
      type: Boolean
    },
    holder: {
      type: String,
      required: true,
      default: ''
    },
    record: {
      type: Object,
      required: true,
      default: () => ({})
    },
    propName: {
      type: String,
      required: true
    },
    propId: {
      type: String,
      required: true
    }
  },
  methods: {
    handleSelect(value, record, option) {
      record[this.propName] = option.componentOptions.propsData.title
      record[this.propId] = value
    },
    handleChange(value, record) {
      const hasValue = this.selectSource.some(item => item.label === value)
      if (!hasValue) {
        record[this.propName] = value
        this.$set(record, this.propId, null)
      }
    }
  }
}
</script>

<style scoped>

</style>

二、页面引用

<template>
    <div>
<a-table
        class="ant-table-sticky"
        bordered
        :columns="columns"
        :dataSource="tableForm.listData"
        :scroll="{ x: 800, y:350}"
        :row-key="(record, index) => { return index }"
        :pagination="false"
        :rowSelection="rowSelection"
      >
<template slot="SteelGradeName" slot-scope="text, record, index, column">
          <a-form-model-item label="" :prop="`listData.${index}.SteelGradeName`" :rules="rules.SteelGradeName">
    <select-auto-complete
              :record="record"
              :select-source="steelSource"
              :disabled="disabled"
              holder="材料"
              prop-name="SteelGradeName"
              prop-id="SteelGradeId"
            ></select-auto-complete>
</a-form-model-item>
        </template>
    </div>
</a-table>
</template>
import SelectAutoComplete from '@/components/SelectAutoComplete'
components: {
    SelectAutoComplete
  }

使用Hooks实现清空AntdAutoComplete组件的值可以通过useRef和useState两个Hooks来实现。 1. 使用useRef创建ref对象: ``` const autoCompleteRef = useRef(null); ``` 2. 将ref对象绑定到AutoComplete组件上: ``` <AutoComplete ref={autoCompleteRef} ... /> ``` 3. 在需要清空组件值的地方,获取组件实例并调用其方法 ``` const handleClear = () => { const autoComplete = autoCompleteRef.current; if (autoComplete) { autoComplete.setState({ value: '' }); } }; ``` 在上述代码中,我们首先使用useRef创建了一个ref对象,并将其绑定到AutoComplete组件上,然后在需要清空组件值的地方,通过autoCompleteRef.current获取AutoComplete组件的实例,最后调用setState方法来清空组件的值。 另外,如果你想要在函数组件中使用AntdAutoComplete组件,可以使用useState来保存AutoComplete组件的实例,然后通过setState方法来清空组件的值。具体操作步骤如下: 1. 使用useState创建状态: ``` const [autoComplete, setAutoComplete] = useState(null); ``` 2. 将setAutoComplete方法绑定到AutoComplete组件上: ``` <AutoComplete ref={setAutoComplete} ... /> ``` 3. 在需要清空组件值的地方,通过autoComplete获取AutoComplete组件的实例,并调用setState方法来清空组件的值: ``` const handleClear = () => { if (autoComplete) { autoComplete.setState({ value: '' }); } }; ``` 在上述代码中,我们通过useState创建了一个状态,并将setAutoComplete方法绑定到AutoComplete组件上,然后在需要清空组件值的地方,通过autoComplete获取AutoComplete组件的实例,最后调用setState方法来清空组件的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值