<template>
<el-select
v-model="value"
filterable
placeholder="请选择"
:filter-method="filterMethod"
@change="valueChange"
@visible-change="visibleChange"
>
<el-option
v-for="item in optionsFilter"
:key="item.value"
:label="item.label"
:value="item.value"
>
<span v-html="showData(item.label)">{{ item.label }}</span>
</el-option>
</el-select>
</template>
<script>
export default {
props: {
options: {
type: Array,
default: () => {
return [];
},
},
},
data() {
return {
optionsFilter: [],
filterValue: "",
value: "",
};
},
watch: {
options() {
this.optionsFilter = JSON.parse(JSON.stringify(this.options));
},
},
methods: {
visibleChange() {
this.filterValue = "";
this.optionsFilter = JSON.parse(JSON.stringify(this.options));
},
filterMethod(val) {
this.filterValue = val;
if (val) {
this.optionsFilter = JSON.parse(JSON.stringify(this.options)).filter(
(v) => {
return v.label
.toLowerCase()
.includes(this.filterValue.toLowerCase());
}
);
} else {
this.optionsFilter = JSON.parse(JSON.stringify(this.options));
}
},
showData(val) {
return val.replace(
new RegExp(this.filterValue, "g"),
`<span style='color: #48a2ff'>${this.filterValue}</span>`
);
},
valueChange() {
this.$emit("changeValue", this.value);
},
},
};
</script>