element-UI中el-select下拉框可搜索时候,filter-method自定义搜索方法

  使用element-UI框架的使用,我们经常使用el-select下拉框,很多时候还需要使用可搜索的下拉框,然后elementUI官网的实例中只是提了一下filter-method可以自定义搜索方法,但是却没有详细介绍怎么用,这里简单介绍一下用法,希望对大家有点帮助

在el-select中加入filterable属性,就开启了搜索功能,然后我们用:filter-method="dataFilter"可以自定义一个搜索筛选条件,在这里来写我们自己的逻辑代码

注意筛选的时候首先要把输入的值赋值给下拉框绑定的变量,否则会筛选会出现问题,代码在下面,自己看一下,不难

然后放示例代码

<template>
<section class="p-10">
<el-select v-model="value" placeholder="请选择" filterable :filter-method="dataFilter">
<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
</el-select>
</section>
</template>
<script>
export default {
data() {
return {
optionsCopy: [{
value: '1',
label: 'meat'
}, {
value: '2',
label: 'drink'
}, {
value: '3',
label: 'food'
}, {
value: '4',
label: '龙须面'
}, {
value: '5',
label: '北京烤鸭'
}],
options: [{
value: '1',
label: 'meat'
}, {
value: '2',
label: 'drink'
}, {
value: '3',
label: 'food'
}, {
value: '4',
label: '龙须面'
}, {
value: '5',
label: '北京烤鸭'
}],
value: ''
};
},
methods: {
dataFilter(val) {
this.value = val;
if (val) { //val存在
this.options = this.optionsCopy.filter((item) => {
if (!!~item.label.indexOf(val) || !!~item.label.toUpperCase().indexOf(val.toUpperCase())) {
return true
}
})
} else { //val为空时,还原数组
this.options = this.optionsCopy;
}
}
}
};
</script>

效果图

嗯,就酱~~

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐