|
導(dǎo)讀微信小程序,簡(jiǎn)稱小程序,英文名Mini Program,是一種不需要下載安裝即可使用的應(yīng)用,它實(shí)現(xiàn)了應(yīng)用“觸手可及”的夢(mèng)想,用戶掃一掃或搜一下即可打開應(yīng)用。小程序是一種不用下載就能使用的應(yīng)用,也是一... 微信小程序,簡(jiǎn)稱小程序,英文名Mini Program,是一種不需要下載安裝即可使用的應(yīng)用,它實(shí)現(xiàn)了應(yīng)用“觸手可及”的夢(mèng)想,用戶掃一掃或搜一下即可打開應(yīng)用。小程序是一種不用下載就能使用的應(yīng)用,也是一項(xiàng)門檻非常高的創(chuàng)新,經(jīng)過將近兩年的發(fā)展,已經(jīng)構(gòu)造了新的小程序開發(fā)環(huán)境和開發(fā)者生態(tài)。 這次給大家?guī)砦⑿判〕绦蜷_發(fā)中怎樣實(shí)現(xiàn)搜索內(nèi)容高亮功能,微信小程序開發(fā)中怎樣實(shí)現(xiàn)搜索內(nèi)容高亮功能的注意事項(xiàng)有哪些,下面就是實(shí)戰(zhàn)案例,一起來看一下。需求 最近在做微信小程序的時(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),高亮效果。 代碼 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
const app = getApp()
Page({
data: {
json: [{ name: 'eiolewkfp', age: 'awdqwwdk', address: 'aueifwhefwfheffoewjowef',aihao:['sdsd','sdfsd','sdsf']}, { name: '98797', age: '6544656', address: '65494364' }], // 可以是任何類型的數(shù)據(jù)
newJson: '',
tempText:''
},
onLoad: function (options) {
this.setData({
newJson:this.data.json
})
},
digui: function (newJson,obj,key) { // 遞歸方法,來遍歷最內(nèi)層的字符串并通過正則來替換
var that = this;
var reg = new RegExp(that.data.tempText,'g');
if (newJson.constructor == Array) {
newJson.forEach(function (item,index) {
if (item.constructor == String){
obj[key].splice(index, 1, item.replace(reg, "<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) { // 這里做全局替換
if(key){
obj[key] = newJson.replace(reg, "<span style='color:red'>" + that.data.tempText + "</span>")
}
}
},
bindKeyInput: function (e) { // 每次輸入來監(jiān)聽鍵盤,處理匹配的數(shù)據(jù)
var text = e.detail.value;
this.setData({
tempText:text
})
var newJson = JSON.parse(JSON.stringify(this.data.json)); // 實(shí)現(xiàn)深復(fù)制
this.digui(newJson);
this.setData({
newJson:newJson
})
}
})相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章! 推薦閱讀: 使用Angular單向數(shù)據(jù)流技巧總結(jié) 以上就是微信小程序開發(fā)中怎樣實(shí)現(xiàn)搜索內(nèi)容高亮功能的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章! 小程序是一種不需要下載安裝即可使用的應(yīng)用,它實(shí)現(xiàn)了應(yīng)用“觸手可及”的夢(mèng)想,用戶掃一掃或者搜一下即可打開應(yīng)用。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!