<CuForm ref="form" :model="form"> <CuFormItem label="设备号" prop="imei" required> </CuForm> <input placeholder="请输入" v-model="form.imei"/> </CuFormItem> <CuFormItem label="安装时间"> <view class="uni-date">{{ form.installTime }}</view> </CuFormItem> <CuFormItem label="安装地点"> <span class="flex1 text-right text-cut">{{ form.address }}</span> </CuFormItem> 如何设备号的输入框改为模糊搜索的输入框 完整代码展示
时间: 2023-07-19 21:23:11 浏览: 203
完整代码如下:
```
<template>
<cu-form ref="form" :model="form">
<cu-form-item label="设备号" prop="imei" required>
<el-autocomplete v-model="form.imei" :fetch-suggestions="querySearch" @select="handleSelect"></el-autocomplete>
</cu-form-item>
<cu-form-item label="安装时间">
<view class="uni-date">{{ form.installTime }}</view>
</cu-form-item>
<cu-form-item label="安装地点">
<span class="flex1 text-right text-cut">{{ form.address }}</span>
</cu-form-item>
</cu-form>
</template>
<script>
import { ref } from 'vue'
import { queryDevices } from '@/api/device'
export default {
setup() {
const form = ref({
imei: '',
installTime: '',
address: ''
})
const querySearch = async (queryString, cb) => {
const res = await queryDevices({ keyword: queryString })
const options = res.data.map(item => ({ value: item.imei, label: item.imei }))
cb(options)
}
const handleSelect = (item) => {
form.imei = item.value
}
return {
form,
querySearch,
handleSelect
}
}
}
</script>
```
其中,我们使用了Vue 3的composition API,并引入了一个queryDevices方法来异步获取设备列表,然后将其转化为el-autocomplete组件可以接受的格式。当用户在el-autocomplete组件中输入关键字时,会通过querySearch方法异步获取匹配的设备列表并显示在下拉选项中。当用户选中某个选项时,会触发handleSelect方法将选中的设备号赋值给form.imei。同时,我们使用了element-ui中的el-autocomplete组件来替换原来的input标签,实现了模糊搜索的输入框。
阅读全文