zoukankan      html  css  js  c++  java
  • 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>

     

    效果图

     

    嗯,就酱~~

  • 相关阅读:
    shell脚本杀掉指定进程下所有子进程(包括子进程的子进程)
    XDebug调试
    PHP基础入门
    猴子补丁(Monkey Patching)
    编写python高质量python代码的59个有效方法
    ubuntu中不能使用终端的情况
    一些个人有用的网站
    Ubuntu将自带的python3升级
    [Vue warn]: You may have an infinite update loop in a component render function.
    OO第四单元总结&期末总结
  • 原文地址:https://www.cnblogs.com/jin-zhe/p/10402294.html
Copyright © 2011-2022 走看看