Vue ant-design select组件搜索和远程数据结合

纯记录
需求:输入“商品”,下拉实时查询内容,选择对应商品内容
效果ant组件实例有

)
相关代码

 <a-form-item name="name">
          <a-select
            v-model:value="formState.name"
            :default-active-first-option="false"
            :filter-option="false"
            :not-found-content="null"
            :options="options"
            :show-arrow="false"
            placeholder="请输入名称"
            show-search
            style="width: 250px"
            @search="handleSearch"
          >
 </a-select>
</a-form-item>
 const options = ref([]);
 const formState = reactive({
  name: null,
 });
 //下拉列表
    const  List = async (str) => {
      const res = await Api.page({
        Title: str,
      });
      res.list.forEach((item) => {
        options.value.push({
          value: item.uuid,
          label: item.name,
        });
      });
    };
    //下拉模糊查询
    const handleSearch = (val) => {
      options.value = [];
      List(val);
    };
   return{
     handleSearch,
   }

升级版(从html到js全部代码)

 <a-form-item label="名称" name="uuid">
    <a-select
        v-model:value="formStateuuid"
        show-search
        placeholder="请输入名称"
        style="width: 300px"
        :filter-option="false"
        :options="state.list"
        @search="loadCollection"
      >
        <template v-if="state.loading" #notFoundContent>
          <a-spin size="small" />
        </template>
     </a-select>
 </a-form-item>
 import { debounce } from "lodash-es";
	//下拉列表
	const state = reactive({
	  list: [],
	  map: {},
	  loading: false,
	});
	//下拉模糊查询
	const load = debounce(async (value) => {
	  if (state.loading) {
	    return;
	  }
	  try {
	    state.loading = true;
	   state.list = [];
	    const res = await  Api.page({
	      Title: value,
	    });
	    const map = {};
	    state.list = res.list?.map((item) => {
	      map[item.uuid] = item;
	      return {
	        value: item.uuid,
	        label: item.name,
	      };
	    });
	   state.map = map;
	  } finally {
	    state.loading = false;
	  }
	}, 500);
	 onMounted(() => {
	    loadPage();
	    load();
	 });
	 return{
	   loadCollection,
	   collectionsState,
	 }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值