vue.js中实现输入框可选可输入模糊匹配下拉框数据关键字高亮满足简拼匹配,全拼匹配,中文匹配三种方式

<el-input
            class="query must-bakcground"
            v-model="queryForm.michead"
            :placeholder="
              $t('message.forecastParcel.qingxuanzekuaidigongsi')
            "
            @focus="focusmichead"
            @blur="blurmichead"
            @input="inputmichead(queryForm.michead)"
          ></el-input>
          <transition name="el-zoom-in-top">
            <div class="michead-list" v-if="CompanYvisable">
              <div class="arrow"></div>
              <ul>
                <li
                  v-for="(item, index) in queryCompany"
                  :key="index"
                  @click="CompanyBtn(item.description)"
                  v-html="item.description"
                ></li>
              </ul>
            </div>
          </transition>

这里是引入第三方的插件通过对下下拉框数据进行检索,找到相关的字符
import pinyinMatch from “pinyin-match”;

这几个是定义在data里面的变量
expressCompany: [],
queryCompany: [],
temporary: [],
CompanYvisable: false,

获取焦点
focusmichead: function () {
      this.CompanYvisable = true;
     这里的参数val是你输入框上绑定的值
      this.busFuzzyMatching(this.queryForm.michead);
    },

    blurmichead: function () {
      setTimeout(() => {
        this.CompanYvisable = false;
      }, 200);
    },

	输入框输入事件
    inputmichead: function (val) {
      this.busFuzzyMatching(val);
    },
	
	//模糊匹配公方法
    busFuzzyMatching: function (val) {
      if (val != "") {
        this.queryCompany = [];
        this.temporary = [];
        let list = JSON.stringify(this.expressCompany);
        let arr = JSON.parse(list);
        arr.forEach((item) => {
          if (
            item.description.indexOf(val) != -1 ||
            item.desc_qp.indexOf(val) != -1 ||
            item.desc_jp.indexOf(val) != -1
          ) {
            this.temporary.push(item);
          }
        });
        if (this.temporary.length != 0) {
          this.temporary.forEach((item) => {
            item.description = item.description.replace(/<.*?>/gi, "");
            item.desc_qp = item.desc_qp.replace(/<.*?>/gi, "");
            item.desc_jp = item.desc_jp.replace(/<.*?>/gi, "");
          });
        }
        this.queryCompany = this.temporary;
        this.queryCompany = [...new Set(this.queryCompany)];
        this.changeColor(this.queryCompany);
      } else {
        this.queryCompany = this.expressCompany;
        this.temporary = [];
      }
    },
	
	下拉框数据点击事件
    CompanyBtn: function (val) {
      this.queryForm.michead = val.replace(/<.*?>/gi, "");
    },

	//模糊匹配高亮的方法
    changeColor(resultsList) {
      if (resultsList && resultsList.length > 0) {
        resultsList.map((item, index) => {
          item.coordinate = pinyinMatch.match(
            item.description,
            this.queryForm.michead
          );

          // 匹配关键字正则
          item.keyscord = item.description.substr(
            item.coordinate[0],
            item.coordinate[1] + 1
          );
          item.replaceReg = new RegExp(item.keyscord, "g");
          item.replaceString =
            '<span style="color:#ff6600;">' + item.keyscord + "</span>";

          if (this.queryForm.michead && this.queryForm.michead.length > 0) {
            item.description = item.description.replace(
              item.replaceReg,
              item.replaceString
            );
          }
        });
        this.queryCompany = resultsList;
        console.log(resultsList);
      } else {
        this.queryCompany = [];
      }
    },
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 Vue.js 的 v-html 指令高亮匹配关键字的示例代码: ```html <template> <div> <input v-model="keyword" placeholder="Search..."> <ul> <li v-for="item in filteredList" :key="item.id"> <span v-html="highlight(item.title)"></span> </li> </ul> </div> </template> <script> export default { data() { return { keyword: '', list: [ { id: 1, title: 'Apple' }, { id: 2, title: 'Banana' }, { id: 3, title: 'Cherry' }, { id: 4, title: 'Durian' }, { id: 5, title: 'Elderberry' }, ], }; }, computed: { filteredList() { return this.list.filter(item => item.title.toLowerCase().includes(this.keyword.toLowerCase())); }, }, methods: { highlight(value) { if (!this.keyword) { return value; } const regex = new RegExp(`(${this.keyword})`, 'gi'); return value.replace(regex, '<span class="highlight">$1</span>'); }, }, }; </script> <style scoped> .highlight { background-color: yellow; } </style> ``` 在这个示例,我们使用 v-html 指令渲染了匹配关键字后的标题,并在页面添加了一个 CSS 类来高亮匹配关键字。在 highlight 方法,我们使用了正则表达式来匹配关键字,并使用 replace 方法将匹配关键字包裹在一个 span 标签,并添加了一个 CSS 类。 需要注意的是,由于使用了 v-html 指令,因此需要确保输入关键字是安全的,以避免 XSS(跨站脚本攻击)漏洞。可以在后端对输入关键字进行过滤和转义,或使用第三方库如 DOMPurify 来过滤输入的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值