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

95 lines
2.2 KiB
Vue
Raw Normal View History

2023-02-17 16:15:01 +08:00
<template>
2023-02-20 10:45:44 +08:00
<el-select
v-model="params[config.querykey]"
filterable
remote
clearable
reserve-keyword
:placeholder="config.placeholder"
:remote-method="remoteMethod"
@clear="list = []"
2023-02-21 13:47:04 +08:00
:disabled="isDisabled"
2023-02-21 16:10:40 +08:00
@change="change"
2023-02-20 10:45:44 +08:00
>
<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-21 13:47:04 +08:00
isDisabled: false,
2023-02-17 16:15:01 +08:00
};
},
2023-02-20 10:45:44 +08:00
watch: {
"config.echoId": {
handler(nval, oval) {
if (nval) {
this.remoteMethod(this.config.echoName);
}
2023-02-21 13:47:04 +08:00
this.isDisabled = !!this.config.isDisabled;
2023-02-20 10:45:44 +08:00
},
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-22 17:21:31 +08:00
if (
this.$utils
.typeJudgment(this.config.autocompleteKey)
.includes("Object")
) {
params[this.config.autocompleteKey.key] = query;
params["enableMark"] = this.config.autocompleteKey.enableMark;
} else {
this.config.autocompleteKey
? (params[this.config.autocompleteKey] = query)
: (params["queryTypeGet"] = 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-21 16:10:40 +08:00
change(val) {
this.$emit("change", val);
},
2023-02-17 16:15:01 +08:00
},
};
</script>
<style lang="scss" scoped></style>