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(() => {
        })
};
相关推荐
RuoyiOffice3 天前
SpringBoot3+Vue3 代码生成器深拆:元数据 IR、模板编译与安全同步
spring boot·vue3·mybatis plus·代码生成器·spring boot 3·velocity·元数据驱动
sir.山3 天前
在 Vue 3 项目中使用 Mock 数据
前端·vue.js·vue3·vite
盛夏绽放6 天前
Proxy与Reflect详解(二):进阶篇——手写Vue3响应式系统
ui·vue3·proxy
RuoyiOffice6 天前
抓包还能看到密码?Vue3 + Spring Boot 3 接口 AES/RSA 加解密(@ApiEncrypt)全链路实战
spring boot·vue3·aes·rsa·接口安全·spring boot 3·apiencrypt
RuoyiOffice8 天前
SpringBoot3+Vue3 接口访问日志实战:@ApiAccessLog 慢接口、参数脱敏与 OperateLog 如何分工
spring boot·vue3·traceid·spring boot 3·访问日志·apiaccesslog·操作日志
tryCbest8 天前
Vue3项目在Nginx中配置代理后端IP
nginx·vue3
glustful10 天前
企业内部审计项目管理 系统开源(springboot+vue3)
vue3·springboot
RuoyiOffice11 天前
SpringBoot3+Vue3 会议室预订系统:时段冲突三模式检测 + 会前定时提醒与 cancelByBiz
spring boot·vue3·flowable·消息中心·spring boot 3·会议室·时段冲突
燐妤12 天前
中老年智能健康管理项目
python·ai·pycharm·vue3·fastapi·项目开发·健康
gis开发之家13 天前
《Vue3 从入门到大神50篇》Vue3 源码详解(二十):生命周期钩子源码解析 —— onMounted / onUpdated 如何实现?
前端·javascript·前端框架·vue3·vue3源码