• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Vue使用ElementUi进行模糊搜索

武飞扬头像
梦无矶
帮助1

ElementUi进行模糊搜索

前言:

在ElementUi中,在带输入建议的输入框中进行搜索,发现只能通过首端匹配,如果输入的是非首字,将无法搜索。

首字搜索

输入豪或者豪大大,可搜索到豪大大香鸡…内容

学新通

非首字搜索

输入鸡,啥也搜不到

学新通

官方函数说明

autocomplete 是一个可带输入建议的输入框组件,fetch-suggestions 是一个返回输入建议的方法属性,如 querySearch(queryString, cb),在该方法中你可以在你的输入建议数据准备好时通过 cb(data) 返回到 autocomplete 组件中。

主要靠的就是如下方法:

querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
          
createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },

函数分析

略微分析一波,额,看不太懂。

仔细分析一波,找到核心突破口,createFilter,中文意思不就是创建过滤嘛,那好办了,直接看这个函数里面的内容。

return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);

理性拆解分析一波

toLowerCase() //用于把字符串转换为小写。
indexOf() //返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。

好家伙,原来在这里,indexof表示首次出现的位置,那么三个等于号加个0是什么。

粗略理解一波,就是必须要找到这个下标且在首位。这个零就是表示所找到的这个index下标必须为0;

而我们需要干嘛?我们需要不管这个字在不在首位,只要在这个字符串里面,那就算找到,这就是我们的模糊搜索的要点。

既然如此,没找到是-1,那么让它大于-1不就可以了?

解决方案

将createFilter方法中的返回方法改成如下,>-1

return (restaurant.host.toLowerCase().indexOf(queryString.toLowerCase()) > -1);

python引申

毕竟,咱做测试的,大部分用的python会多亿些,那么看js代码可能会像我一样云里雾里,扒出js的indexOf源码瞅一眼。

indexOf(searchString: string, position?: number): number;

    /**
     * Returns the last occurrence of a substring in the string.
     * @param searchString The substring to search for.
     * @param position The index at which to begin searching. If omitted, the search begins at the end of the string.
     */

这是不是让你联想到了python里面的find方法?

那让我们看一下find的源码

    def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
        """
        S.find(sub[, start[, end]]) -> int
        
        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.
        
        Return -1 on failure.
        """
        return 0

不愧是python,说的傻子都能看懂了。

找到了返回最开始找到的下标值,没找到返回-1嘛这不是。

来个小案列巩固一下。

all = 'text_xiaozaixt'
part = 'xt'
notin = 'm'

print(all.find(part)) # 2 表示首次出现的位置下标为2
print(all.find(notin)) # -1
print("-----------------")
# python indexOf
if (all.find(part) > -1):
    print(f"{part}找到了")
else:
    print(f"{part}不在name里面")

if (all.find(notin) > -1):
    print(f"{notin}找到了")
else:
    print(f"{notin}不在name里面")

学新通
# ------------运行结果----------------
xt找到了
m不在name里面

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghhcj
系列文章
更多 icon
同类精品
更多 icon
继续加载