实现这个功能,在网上找了很多感觉都不太准确,残缺不全,这里做个总结
完整代码
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearchAsync"
placeholder="请输入内容"
@select="handleSelect">
</el-autocomplete>
<script>
export default {
data() {
return {
restaurants: [],
state: '',
}
}
mounted() {
this.loadAll()
},
methods: {
loadAll() {
let that = this
that.$axios.get(that.baseURL+'PROD/testdialog/documents/')
.then(function(res){
if (res.data.code===1){
that.restaurants = res.data.data
}else{
that.$message.error(res.data.msg)
}
})
.catch(function(err){
console.log(err)
})
},
querySearchAsync(queryString, cb) {
var restaurants = this.restaurants;
var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
cb(results);
},
createStateFilter(queryString) {
return (state) => {
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1);
};
},
handleSelect(item) {
console.log(item);
},
}
</script>
这里要强调两点: (1)
createStateFilter(queryString) {
return (state) => {
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1);
};
},
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1);
(2)
createStateFilter(queryString) {
return (state) => {
return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1);
};
|