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

104 lines
2.6 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) {
2023-02-24 17:12:47 +08:00
this.list = [];
this.isDisabled = !!this.config.isDisabled;
2023-02-20 10:45:44 +08:00
if (nval) {
2023-02-24 17:12:47 +08:00
let resultName = this.config.echoName.replace(/\s*/g, "");
if (!resultName) return;
this.remoteMethod(resultName);
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-22 17:37:16 +08:00
let type = this.$utils.typeJudgment(this.config.autocompleteKey);
2023-02-23 16:08:14 +08:00
// if (!type.includes("Object") && !type.includes("String")) {
// this.loading = false;
// this.$message.warning("config.autocompleteKey格式有误");
// return;
// }
let params = {};
2023-02-22 17:37:16 +08:00
if (type.includes("Object")) {
2023-02-22 17:21:31 +08:00
params[this.config.autocompleteKey.key] = query;
params["enableMark"] = this.config.autocompleteKey.enableMark;
2023-02-22 17:37:16 +08:00
}
2023-02-23 16:08:14 +08:00
if (
type.includes("String") ||
type.includes("Undefined") ||
type.includes("Null")
) {
// 接口为get类型时需将autocompleteKey值设为空
console.log();
2023-02-22 17:21:31 +08:00
this.config.autocompleteKey
? (params[this.config.autocompleteKey] = query)
2023-02-23 16:08:14 +08:00
: (params = query);
2023-02-22 17:21:31 +08:00
}
2023-02-17 16:15:01 +08:00
this.config.serveTarget(params).then((res) => {
2023-02-23 16:08:14 +08:00
this.loading = false;
if (res.data.length) {
this.list = res.data;
} else this.list = [];
2023-02-17 16:15:01 +08:00
});
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>