在实际工作中,我们会遇到很多的模糊查询以及查询功能,今天做个记录,用vue实现简易版的模糊查询
<template> <div> <van-search v-model="value" @input="moduleChange" placeholder="请输入搜索关键词" /> <div> <ul> <li v-for="(item,index) in list" :key="index"> <span :style="{color:item.color?'red':''}">{{item.name}}</span> </li> </ul> </div> </div> </template> <script lang="ts"> import { cloneDeep } from "lodash"; import { Component, Vue } from "vue-property-decorator"; @Component export default class Menu extends Vue { public value = ""; public arr: { name: string; id: number; color?: boolean | undefined }[] = [ { name: "张三", id: 1, color: false, }, { name: "李四", id: 2, color: false, }, { name: "测试", id: 2, color: false, }, ]; public list = cloneDeep(this.arr); public moduleChange(val: string) { const newArr = cloneDeep(this.arr); let reg = new RegExp(val); newArr.map((i) => { i.color = i.name.match(reg) && this.value ? true : false; }); const searArr = newArr.find((el: any) => val === el.name); this.list = searArr ? [searArr] : newArr; } } </script> <style scoped> </style>