效果如下:
话不多说上代码:
因为el-select的数据模式是value和label形式,value就是选择的实际值,lable是展示
generateArray() {
const start = 1000; // 开始值
const end = 500000; // 结束值
const array = [];
for (let i = start; i <= end; i += 1000) {
const formattedValue = this.numberWithCommas(i.toFixed(2));
const item = {
value: i.toString(),
label: formattedValue,
};
array.push(item);
}
return array;
},
//金额加逗号 三位
numberWithCommas(value) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
<el-select
v-model="money"
placeholder="请选择"
style="width: 80%"
clearable
filterable
:filter-method="handleFilter"
@change="changeMoney"
@focus="handleFocus"
@visible-change="handleVisibleChange"
>
<el-option
v-for="item in MoneyOptions"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
// 自定义搜索
handleFilter(query) {
this.MoneyOptions= this.options.filter((item) => {
// 这里是用的value选项筛选,默认是label
return item.value.indexOf(query) > -1;
});
},
这里用到el-select的focus和visible-change和change方法,点中时把数据恢复
// 恢复数据
handleFocus() {
this.MoneyOptions= [...this.options]
},
handleVisibleChange(visible){
if(!visible){
this.handleFocus()
}
},
changeMoney(){
this.handleFocus()
},
这样子效果就出现了