<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 07:23:10 浏览: 112
jquery中$(#form :input)与$(#form input)的区别
如果想将设备号的输入框改为模糊搜索的输入框,可以使用element-ui中的el-autocomplete组件。首先需要引入el-autocomplete组件,然后将原来的input标签替换为el-autocomplete标签,并设置其v-model绑定的值为form.imei,然后在el-autocomplete标签中设置:fetch-suggestions属性指定一个异步方法来获取模糊匹配的选项列表,最后在el-autocomplete标签中设置:select方法指定选中某个选项时的操作。具体代码如下:
```
<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。
阅读全文