示例如图
el-select上绑定remote-method
属性
javascript
<el-select v-model="checkForm.type" filterable remote reserve-keyword :remote-method="remoteMethod" :loading="loading">
<el-option v-for="item in options" :key="item.value" :value="item.value">
<span v-html="item.text"></span>
</el-option>
</el-select>
定义remoteMethod方法
javascript
interface ListItem {
value: string
label: string,
text: string
}
const options = ref<ListItem[]>([])
const list = ref([{
value:'123',
label:'123',
text:''
},{
value:'125',
label:'125',
text:''
}])
const remoteMethod = (query) => {
if (query) {
loading.value = true
setTimeout(() => {
loading.value = false
list.value.forEach(item => {
if(item.value.indexOf(query)>=0){
var val = item.value.split(query)
item.text = val.join('<span style="color:red">' + query + "</span>")
}
})
options.value = list.value
}, 1000)
} else {
options.value = []
}
}