el-select 支持拼音搜索(含插件 pinyin-match 的使用)

简介: el-select 支持拼音搜索(含插件 pinyin-match 的使用)

效果预览

安装插件 pinyin-match

cnpm i pinyin-match --save

导入插件 pinyin-match

import PinYinMatch from "pinyin-match";

在过滤方法中使用

    matchFruit(searchContent) {
      if (searchContent) {
        let result = [];
        this.list_org.forEach((item) => {
          // matchResult 的值为 true/false
          let matchResult = PinYinMatch.match(item.label, searchContent);
          if (matchResult) {
            result.push(item);
          }
        });
        this.list_filtered = result;
      } else {
        this.list_filtered = this.list_org;
      }
    },

完整范例

<template>
  <div class="container">
    <el-select
      v-model="fruit"
      placeholder="请选择水果"
      filterable
      :filter-method="matchFruit"
      clearable
      @clear="clearFruit"
    >
      <el-option
        v-for="item in list_filtered"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
import PinYinMatch from "pinyin-match";
export default {
  data() {
    return {
      fruit: "",
      list_org: [
        {
          value: 1,
          label: "苹果",
        },
        {
          value: 2,
          label: "香蕉",
        },
        {
          value: 3,
          label: "梨子",
        },
      ],
      list_filtered: [],
    };
  },
  mounted() {
    this.list_filtered = this.list_org;
  },
  methods: {
    matchFruit(searchContent) {
      if (searchContent) {
        let result = [];
        this.list_org.forEach((item) => {
          // matchResult 的值为 true/false
          let matchResult = PinYinMatch.match(item.label, searchContent);
          if (matchResult) {
            result.push(item);
          }
        });
        this.list_filtered = result;
      } else {
        this.list_filtered = this.list_org;
      }
    },
    clearFruit() {
      this.list_filtered = this.list_org;
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  margin: 30px;
}
</style>

注意事项

  • el-option 的 v-for 循环中的 key 不能用 index, 否则会出现输入拼音不显示的bug,如输入 l 不显示
  • 若 el-select 添加了 clearable,则需如范例添加 @clear=“clearFruit” 以便在清除内容后,将下拉列表恢复为过滤前的列表。
目录
相关文章
|
3天前
|
前端开发
ElementPlus中total出现el.pagination.total,显示总数没有出现怎样解决,出现的是英文,不是中文如何解决,这里如何配置中文,配置中文
ElementPlus中total出现el.pagination.total,显示总数没有出现怎样解决,出现的是英文,不是中文如何解决,这里如何配置中文,配置中文
|
24天前
|
自然语言处理 Java Maven
word分词--实例---用法
word分词--实例---用法
14 2
|
2月前
|
移动开发 前端开发 JavaScript
uVIew Search 搜索
uVIew Search 搜索
31 0
|
11月前
|
Python
Python搜索与匹配绝技:掌握search()和match()从零到高手
Python搜索与匹配绝技:掌握search()和match()从零到高手
79 0
|
10月前
|
JavaScript
jquery模糊查询--搜索demo效果示例(整理)
jquery模糊查询--搜索demo效果示例(整理)
33.从入门到精通:Python3 正则表达式 re.match函数 re.search方法 re.match与re.search的区别
33.从入门到精通:Python3 正则表达式 re.match函数 re.search方法 re.match与re.search的区别
|
搜索推荐
item_search - 按关键字搜索商品
一:便捷和快速。使用关键词检索可以很快地找到所需要的信息,只需要输入关键词,搜索引擎就会返回相关结果。 二:范围广。关键词检索可以搜索全平台范围内的网页和相关信息,用户能够快速获取信息。 三:检索准确度高。通过对关键词的筛选和搜索引擎的智能推荐,用户可以获得更加准确匹配的搜索结果。
|
Java PHP C#
item_search-根据关键词取文章列表
item_search-根据关键词取文章列表
一日一技:在ES中如何使用通配符搜索keyword字段
一日一技:在ES中如何使用通配符搜索keyword字段
188 0
|
搜索推荐 关系型数据库 数据库
使用sphinx search打造你自己的中文搜索引擎
Google搜索引擎建立至今已经快20年了,之后全球各类大大小小类似的搜索引擎也陆续出现、消亡。国内目前以百度为大,搜狗、360、必应等也势在必争。搜索引擎技术也发展的相当成熟,同时也就出现了很多开源的搜索引擎系统。
3100 0