Files
refinery-admin/src/components/autocomplete/index.vue

105 lines
2.5 KiB
Vue
Raw Normal View History

2023-02-17 16:15:01 +08:00
<template>
2023-02-20 10:45:44 +08:00
<!-- <el-autocomplete
2023-02-17 16:15:01 +08:00
v-model="text"
:fetch-suggestions="querySearchAsync"
:value-key="config.valueKey"
:placeholder="config.placeholder"
clearable
@select="selectAutocomplete"
@clear="params[config.querykey] = ''"
2023-02-20 10:45:44 +08:00
></el-autocomplete> -->
<el-select
v-model="params[config.querykey]"
filterable
remote
clearable
reserve-keyword
:placeholder="config.placeholder"
:remote-method="remoteMethod"
@clear="list = []"
>
<el-option
v-for="(item, index) in list"
:key="item.id + index"
:label="item[config.labelKey]"
:value="item[config.valueKey]"
>
</el-option>
</el-select>
2023-02-17 16:15:01 +08:00
</template>
<script>
export default {
props: {
params: Object,
config: Object,
// config: {
2023-02-20 10:02:33 +08:00
// serveTarget: {}, // 远程搜索接口
2023-02-20 10:45:44 +08:00
// autocompleteKey: "", //远程搜索接口参数名
// labelKey: "", //接口返回数据label名
// valueKey: "", //接口返回数据唯一值
2023-02-17 16:15:01 +08:00
// querykey: "", //查询接口参数名
// },
},
data() {
return {
text: "",
2023-02-20 10:45:44 +08:00
list: [],
2023-02-17 16:15:01 +08:00
};
},
2023-02-20 10:45:44 +08:00
watch: {
"config.echoId": {
handler(nval, oval) {
if (nval) {
console.log(nval);
this.remoteMethod(this.config.echoName);
}
},
deep: true,
immediate: true,
},
2023-02-17 16:15:01 +08:00
},
methods: {
// 远程搜索
2023-02-20 10:45:44 +08:00
remoteMethod(query) {
if (query !== "") {
this.loading = true;
2023-02-17 16:15:01 +08:00
let params = {};
2023-02-20 10:45:44 +08:00
params[this.config.autocompleteKey] = query;
2023-02-17 16:15:01 +08:00
this.config.serveTarget(params).then((res) => {
let timeInstance = setTimeout(() => {
2023-02-20 10:45:44 +08:00
this.loading = false;
2023-02-17 16:15:01 +08:00
clearTimeout(timeInstance);
if (res.data.length) {
2023-02-20 10:45:44 +08:00
this.list = res.data;
} else this.list = [];
2023-02-17 16:15:01 +08:00
}, 1000 * Math.random());
});
2023-02-20 10:45:44 +08:00
} else {
this.list = [];
}
2023-02-17 16:15:01 +08:00
},
2023-02-20 10:45:44 +08:00
// querySearchAsync(queryString, cb) {
// if (queryString) {
// let params = {};
// params[this.config.autocompleteKey] = queryString;
// this.config.serveTarget(params).then((res) => {
// let timeInstance = setTimeout(() => {
// clearTimeout(timeInstance);
// if (res.data.length) {
// cb(res.data);
// } else cb([]);
// }, 1000 * Math.random());
// });
// } else cb([]);
// },
// selectAutocomplete(item) {
// this.params[this.config.querykey] = item.id;
// },
2023-02-17 16:15:01 +08:00
},
};
</script>
<style lang="scss" scoped></style>