首页 > 其他分享 >vue3 通过fuse.js 实现前端模糊查询

vue3 通过fuse.js 实现前端模糊查询

时间:2023-05-08 15:35:33浏览次数:33  
标签:const selected value js fuse vue3 匹配 props

在项目中写好多个查询组件,基于 element-plus el-select 组件:

举个栗子,SelectAllCompany.vue:

<template>
    <!-- 获取客户下拉数据,type 0 有限公司 -->
    <el-select v-model="current" :multiple="multiple" remote :remote-method="querySearch" :suffix-icon="loading ? '' :''" :loading="loading" loading-text="数据加载中..." filterable clearable placeholder="搜索客户名称/法人、负责人姓名/手机号" :style="{'width':width || '300px'}">
        <template #empty>
            <div style="font-size:14px;padding:30px">
                未找到相关客户信息,请前往 <el-button link type="primary" @click="router.push('/customer/index')">客户管理</el-button> 中添加
            </div>
        </template>
        <el-option
            v-for="option in options"
            :key="option.item.customerId"
            :label="option.item.name"
            :value="option.item[searchBy || 'customerId']">
        </el-option>
    </el-select>
</template>

<script setup>
import {getList as getAllCustomers} from "@/api/customer";
import Fuse from 'fuse.js'
const { proxy } = getCurrentInstance()
const props = defineProps(['value','searchBy','multiple','width'])
const emits = defineEmits(['update:value','selected'])
// 使用路由
const router = useRouter()
const fuse = ref(undefined); // 模糊查询插件
const list = ref([])
const loading = ref(false)
const options = ref([])
const current = computed({
  get: () => props.value,
  set: (val) => {
    emits('update:value', val)
  }
});
watch([()=>current.value, ()=>list.value],([newVal, newList])=>{
    if(!props.multiple){
        const selected = newList.find(z=>z[props.searchBy || 'customerId'] == newVal)
        emits('selected',selected)
        if(fuse.value){
            querySearch(newVal || '')
        }
    }else{
        const selected = newList.filter(z=>newVal.includes(z[props.searchBy || 'customerId']))
        emits('selected',selected)
    }
})
// 获取数据
const getList =(value)=>{
    const queryParams= {
        status: "0", // 正常
        checkStatus:"1", // 审核通过
        customerType: '0', //0 有限公司 1 个体户
        customerName:value // 搜索名字
    }
    loading.value = true
    getAllCustomers(queryParams).then(res=>{
        if (res.code = 200){
            list.value = res.data || []
            initFuse()
            querySearch(current.value || '')
        }else{
            proxy.$modal.msgError(res.msg || "请求发生错误,请稍后重试")
        }
    }).finally(()=>{
        loading.value = false
    })
}
// 初始化模糊查询插件
const initFuse =()=> {
    fuse.value = new Fuse(list.value, {
        shouldSort: true,
        threshold: 0.4, // 匹配算法阈值。阈值为0.0需要完全匹配(字母和位置),阈值为1.0将匹配任何内容。
        /**
         * 确定匹配与模糊位置(由位置指定)的距离。一个精确的字母匹配,即距离模糊位置很远的字符将被视为完全不匹配。
         *  距离为0要求匹配位于指定的准确位置,距离为1000则要求完全匹配位于使用阈值0.8找到的位置的800个字符以内。
         */
        location: 0,// 确定文本中预期找到的模式的大致位置。
        distance: 100,
        minMatchCharLength: 1,
        // 查询字段
        keys: ['customerId','customerName','clientContacts','clientPhones','legalName','legalPerson','legalTel']
    })
}
const querySearch =(query)=> {
  if (query !== '') {
    options.value = fuse.value.search(query)
  } else {
    options.value = []
  }
}
onMounted(()=>{
    getList()
})
</script>

<style>

</style>

标签:const,selected,value,js,fuse,vue3,匹配,props
From: https://www.cnblogs.com/kitty-blog/p/17381903.html

相关文章

  • C# .net core 返回json 中文字符编码被转换或乱码问题
    开发环境VS2022+.NET6.0现象接口返回Json中文数据时出现乱码。例如后台返回结果:"0506133015\u56FE\u8868\u9009\u62E9.png"。解决办法以下方法任选其一即可。//方法1:在Program.cs中添加以下代码varbuilder=WebApplication.CreateBuilder(args);builder.Services.Ad......
  • vue3 证件识别上传组件封装
    证件图片识别上传根据业务需要,经常涉及到证件上传,例如身份证上传、银行卡、营业执照等信息,根据设计师的设计,单独封装了一个上传组件。识别接口后端用的是阿里云的。上传组件用的是element-plusel-upload上代码:<template><divclass="component-upload-image"><el-up......
  • The connection to the server localhost:8080 was refused - did you specify the ri
    遇到如下问题:[root@k8s-node1~]#kubectlgetpodTheconnectiontotheserverlocalhost:8080wasrefused-didyouspecifytherighthostorport?解决方式:cd/etc/kubernetes/查看到有个文件:kubelet.conf(你们的有可能是admin.conf)执行命令echo"exportKUBECONFIG......
  • 常用js方法函数
    获取字符串长度(英文1个,中文2个)//把双字节的字符替换成两个单字节的字符,并获取它的字符数letstr='你好115s'constlen=str.replace(/[^\x00-\xff]/g,"00").length;console.log(len)......
  • 使用 Node.js 内置调试器进行调试
    使用Node.js内置调试器进行调试调试是一个多阶段的过程,通常遵循以下步骤:识别程序中的bug。查找bug在代码中的位置。分析bug发生的原因。修复bug。验证修复是否有效。在Node.js程序中发现错误后,你面临的第一个挑战就是在代码中找到错误所在的位置。为实现这一......
  • 缺少Jackson jar包,导致对象无法转化为json数据输出
       用于Json的序列化(serialization)和反序列化(deserialization)。Jackson包含三个包jackson-core、jackson-annotation、jackson-databind,作用如下  <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</a......
  • jstat使用实用教程
    大概阅读5分钟,本教程非常实用,清晰案例展示,建议收藏查询要监控的java进程号(pid)参考@[toc]jstat简介Jstat位于java的bin目录下,主要利用JVM内建的指令对Java应用程序的资源和性能进行实时的命令行的监控,包括了对Heapsize和垃圾回收状况的监控。Jstat可以用来监视VM内存内的各种堆和......
  • js中class的构造函数的this指向问题
    场景定义了一个类的构造函数需要的参数是一个对象,而该对象的属性对class实例的属性进行了调用或修改,也就是在对象中使用了this问题示例classPerson{name:''age:0sex:0setName:nullconstructor(person){this.setName=person.setName}}con......
  • 在JS中如何判断两个对象是否相等
    在JavaScript中,判断两个对象是否相等有多种方法,取决于你对相等的定义以及对象属性的类型。以下是几种常见的方法: 1.严格相等运算符(===)使用===运算符可以比较两个对象是否引用同一个对象。如果两个变量引用了同一个对象,则它们是相等的,否则它们是不相等的。例如:const......
  • Node.js躬行记(28)——Cypress自动化测试实践
    最近在研究如何提升项目质量,提炼了许多个用于自测的测试用例,但是每次修改后,都手工测试,成本太高,于是就想到了自动化测试。在一年前已将Cypress集成到管理后台的项目中,不过没有投入到实践中。今天在实践时发现,版本已经到了12.X,当时集成的版本是8.X。一、准备在......