需求:项目中用到input框 需要远程搜索,从服务器获取数据,看到element 中有相关组件,在此记录下,在vue项目中如何使用远程搜索来实现的
组件:el-autocomplete
页面实现效果:
代码如下:
<el-autocomplete
ref="autocomplete"
:popper-append-to-body="false"
clearable
class="filter-item"
v-model="scanBlurry"
:fetch-suggestions="querySearchAsync"
:placeholder="$t('barcode.scanSearch')"
@keyup.enter.native="getScanSearch($event)"
></el-autocomplete>
data中定义:
loading: false,
optionsMatching: [],
timeout: null,
scanBlurry: "",
methods方法:
methods: {
querySearchAsync(queryString, cb) {
var results = [];
clearInterval(this.timeout);
this.timeout = setTimeout(() => {
searchMatching(queryString).then((res) => {
var result = res;
//循环放到一个远程搜索需要的数组
this.optionsMatching = result.map((item, index) => {
return {
value: item,
index: index,
};
});
results = this.optionsMatching;
cb(results);
});
}, 100);
},
}
// 料盒物料信息enter事件
getScanSearch() {
const query = {
barcode: this.resBarcode,
operatePN: this.scanBlurry,
};
operatePos(query).then((res) => {
if (res.code == "0") {
this.showInfoMsg(window.vm.$i18n.t("tips.add"), "success");
this.getChange();
}
});
},
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/79310.html