需求
最近在做微信小程序的時(shí)候既琴,需要實(shí)現(xiàn)在搜索框的輸入內(nèi)容的時(shí)候?qū)崿F(xiàn)全局匹配實(shí)現(xiàn)高亮效果,目前的思路是栏渺,遞歸后臺(tái)來返回的數(shù)據(jù)呛梆,并將對(duì)象的value值替換為需要的dom節(jié)點(diǎn),并且通過rich-text來實(shí)現(xiàn)磕诊,高亮效果填物。
思路
在實(shí)現(xiàn)的過程中主要考慮,不同類型的數(shù)據(jù)結(jié)構(gòu)霎终,過濾掉特殊符號(hào)滞磺,url這些基本需求;同時(shí)在實(shí)現(xiàn)的過程中每次都要去處理最原始的數(shù)據(jù)莱褒,這就需要考慮到對(duì)象的深拷貝問題击困,目前所采用的方法是通過JSON.parse(JSON.stringify(str))來處理,因?yàn)樵谶@個(gè)全局搜索的項(xiàng)目中不太會(huì)用到函數(shù)這些對(duì)象广凸。最后通過replace方法來處理這些目標(biāo)字符串阅茶。
截圖
代碼
wxml:
<view class='homePage'>
<input bindinput="bindKeyInput"></input>
<view wx:for="{{newJson}}" wx:for-item='item' wx:key>
<rich-text nodes="{{item.name}}"></rich-text>
<rich-text nodes="{{item.address}}"></rich-text>
<rich-text nodes="{{item.age}}"></rich-text>
<view wx:if="{{item.aihao}}" wx:for="{{item.aihao}}" wx:for-item='sitem' wx:key>
<rich-text nodes="{{sitem}}"></rich-text>
</view>
</view>
</view>
js:
//index.js
//index.js
const app = getApp()
Page({
data: {
homeUrl: '../index/index',
mineUrl: '../mine/mine',
newFillUrl: '../newFill/newFill',
historyUrl: '../historyData/historyData',
json: [{ name: '你是誰', age: 'awdqww\\k', address: 'auemnkjkifwh{\\efwfheffoewjowef', aihao: ['sdsd', 'sdfsd', 'sdsf'] }, { name: '98797', age: '6544656', address: 'https://www.baidu.com/' }],
newJson: '',
tempText: '',
showShadow: false,
chartNumber: 0,
newStr:''
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
this.setData({
newJson: this.data.json
})
},
haha: function () {
console.log('haha');
wx.navigateTo({
url: '../mine/mine',
})
},
digui: function (newJson, obj, key) {
var urlReg = new RegExp('(https ?|ftp | file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]')
var that = this;
var reg = that.data.tempText;
if (that.data.tempText == '.' || that.data.tempText == '\\' || that.data.tempText == '\?' || that.data.tempText == '\[' || that.data.tempText == '\]') {
reg = '\\' + that.data.tempText
}
var reg = new RegExp(reg, 'ig');
if (newJson.constructor == Array) {
newJson.forEach(function (item, index) {
if (item.constructor == String && !urlReg.test(item)) {
obj[key].splice(index, 1, item.replace(reg, function (index) {
if (that.data.newStr != ''){
that.setData({
chartNumber: (that.data.chartNumber + 1)
})
}
return "<span style='color:red'>" + that.data.tempText + "</span>"
}))
} else {
that.digui(item, newJson);
}
});
} else if (newJson.constructor == Object) {
var json = {};
for (var key in newJson) {
json[key] = newJson;
that.digui(newJson[key], newJson, key);
}
} else if (newJson.constructor == String && !urlReg.test(newJson)) { // 這里做全局替換
if (key) {
obj[key] = newJson.replace(reg, function () {
if (that.data.newStr != '') {
that.setData({
chartNumber: (that.data.chartNumber + 1)
})
}
return "<span style='color:red'>" + that.data.tempText + "</span>"
})
}
}
},
showBgShadow: function (e) {
this.setData({
showShadow: e.detail.showBgShadow
})
},
bindKeyInput: function (e) {
var regChart = this.data.regChart;
var text = e.detail.value;
var newStr = '';
newStr = text.replace(/[\@\#\$\%\^\&\*\{\}\:\"\L\<\>\?\\\.]/, '')
this.setData({
tempText: newStr,
chartNumber: 0,
newStr: newStr
})
var newJson = JSON.parse(JSON.stringify(this.data.json));
this.digui(newJson);
this.setData({
newJson: newJson
})
}
})