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(() => {
        })
};
相关推荐
Modify_QmQ1 天前
Three.js 实战【3】—— 城市模型渲染
3d·vue3·three·glbf
山水阳泉曲3 天前
开发后台管理系统-开发环境搭建
vue3·cdn·后台管理·从零开始
陈逸子风3 天前
.net core8 使用JWT鉴权(附当前源码)
vue3·webapi·权限·流程
小许_.6 天前
vite+vue3快速构建项目+router、vuex、scss安装
前端·css·vue3·vite·scss
行思理7 天前
UniApp 从Vue2升级为Vue3需要注意哪些方面
javascript·vue.js·uni-app·vue3·vue2
唯之为之9 天前
vue3项目部署到Github
vue·github·pnpm·vue3·vite
一雨方知深秋9 天前
vue3 项目中使用git
css·git·gitee·html·vue3
陈逸子风9 天前
.net core8 使用Swagger(附当前源码)
vue3·webapi·权限·流程
奶昔不会射手9 天前
vue3项目,本地页面正常显示,打包后页面空白
vue3·vue-router
Modify_QmQ9 天前
leaflet【十】实时增加轨迹点轨迹回放效果实现
vue3·leaflet·轨迹回放·实时增加轨迹