js实现模糊搜索与关键字高亮

时间:2025-03-29 20:43:14
//请求全部数据 getResults(){ uni.request({ url: url, method: "POST",//请求方式 header: { 'X-JWT-TOKEN': this.token }, data: {请求的参数}, success: (res) => { uni.hideLoading(); this.dataList = res.data.Results; } }); } //输入文字时 filterData: function(e) { var key = e.target.value; this.keywords = key; let arraylist = []; // 只查找二字码和中文名字,模糊搜索 arraylist = this.dataList.filter(function(item) { return (item.FeeCode.toLowerCase().indexOf(key.toLowerCase()) >= 0 || item.FeeName.toLowerCase().indexOf(key.toLowerCase()) >= 0 ); }).sort((a, b) => a.FeeCode.localeCompare(b.FeeCode));//转为英文字母大写比对 // 关键字高亮 let resultList = []; arraylist.forEach((item) => { const reg = new RegExp(key, "ig"); //全局替换 let obj = { FeeCode: item.FeeCode, FeeID: item.FeeID, FeeName: item.FeeName, Text: item.Text.replace(reg, (val) => `<span style='color:#0076D6'>${val}</span>`) }; resultList.push(obj); }); this.filterList = resultList; },