a-auto-complete 请求后端数据做模糊查询,解决下拉框选择选不上,不回显的问题

a-auto-complete 请求后端数据做模糊查询,解决下拉框选择选不上,不回显的问题

记录一个a-auto-complete卡bug卡了两天,找不到哪里的问题下拉框选择选不上,不回显,最后终于解决了。

我还对下拉框显示的内容做了小调整。

直接看代码吧。

复制代码
<a-auto-complete v-model:value="inputValue" :options="personOptions" style="width: 300px" placeholder="请输入姓名"
            @select="onSelect" @search="onSearch">
            <template #option="item">
                <span>{{ item.name }}</span><br /><span style="color:#1890ff">{{ item.licenseNumber }}</span>
            </template>
        </a-auto-complete>
        ......
        ......
        ......
//input值
const inputValue = ref('');
//下拉框option
const personOptions = ref([]);
//输入的事件
const onSearch = searchText => {
//发送请求获取option数组
    const param = {
        name: searchText
    }
    relationApi.getPerson(param)
        .then((res) => {
        ///卡bug的地方就在这,请求接口返回的数据了没有value这个字段,所以要给option数组里的对象添加value属性
        ///option数组里需要name和value属性!
            const a = res.map(item => {
                return {
                    ...item,
                    value: item.name
                }
            })
            personOptions.value = !searchText
                ? []
                : a;
        })
        .finally(() => {
        })
};
//选择下拉框的事件
const onSelect = (value, option) => {
/value是下拉框选中的值,option是选中的所有属性,可以取你自己想要的值,我这里取的是option.licenseNumbe,然后自己进行后续操作。
    relationApi.getPersonDetial({ licenseNumber: option.licenseNumber })
        .then((res) => {
            if (res.body) {
                treeData.value = res.body
                
            } else {
                message.warning('暂无数据!')
                treeData.value = []
            }
            initTree();
        })
        .finally(() => {
        })
};
相关推荐
红红谈说4 分钟前
同一套产品两个技术栈,双版本前端工程差异怎么收敛?
前端·uni-app·vue3·前后端分离·双版本维护·接口约定
RuoyiOffice1 天前
Flowable 状态通知设计:单体用本地事件,微服务如何回写业务状态?
spring boot·微服务·vue3·flowable·ruoyi office·redis stream·流程状态通知
AI开源软件1 天前
SpringBoot + Vue3 人力资源管理系统推荐:组织、档案与移动办公
vue3·springboot·ruoyi·oa系统·移动办公·人力资源管理系统·hrm
梅雅达编程笔记1 天前
Day 17 | uni-app 小程序:前端从零搭建
微信小程序·typescript·uni-app·vue3·pinia·前端开发·全栈
RuoyiOffice2 天前
项目预算和交付日期变了,为什么不能直接改台账?
spring boot·项目管理·vue3·flowable·业务建模·ruoyi office·项目变更
AI源码2 天前
图书馆借阅管理系统
vue3·springboot
RuoyiOffice3 天前
SpringBoot+Vue3 单据校验设计:保存草稿、提交审批、退回重填,各该检查什么?
spring boot·vue3·flowable·表单设计·ruoyi office·草稿保存·单据校验
RuoyiOffice5 天前
SpringBoot+Vue3 业财协同架构:预算、费用分摊、发票、往来与凭证怎样避免五套数字
spring boot·vue3·财务管理·spring boot 3·预算管理·ruoyi office·业财协同
RuoyiOffice6 天前
Vue3+TypeScript 企业管理平台统一UI:新增一个模块,为什么还要重新调样式?
spring boot·typescript·vue3·组件化开发·企业管理软件·ruoyi office·统一ui
Liu.7749 天前
Vue3 中如何使用 GET 请求传递复杂参数
vue.js·vue3