This time I will show you how to implement the search content highlighting function in the development of WeChat mini programs. What are the precautions for how to implement the search content highlighting function in the development of WeChat mini programs? Here is a practical case, let’s take a look.
Requirements
When I was working on a WeChat applet recently, I needed to achieve global matching and highlight effects when entering content in the search box. The current idea is , recursively return the data in the background, replace the value of the object with the required dom node, and achieve the highlighting effect through rich-text.
Code
wxml:
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' }], // 可以是任何类型的数据 newJson: '', tempText:'' }, onLoad: function (options) { this.setData({ newJson:this.data.json }) }, digui: function (newJson,obj,key) { // 递归方法,来遍历最内层的字符串并通过正则来替换 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, "" + that.data.tempText + "")) }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, "" + that.data.tempText + "") } } }, bindKeyInput: function (e) { // 每次输入来监听键盘,处理匹配的数据 var text = e.detail.value; this.setData({ tempText:text }) var newJson = JSON.parse(JSON.stringify(this.data.json)); // 实现深复制 this.digui(newJson); this.setData({ newJson:newJson }) } })
I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use CDN to optimize project loading speed
Summary of techniques for using Angular one-way data flow
The above is the detailed content of How to implement search content highlighting function in WeChat applet development. For more information, please follow other related articles on the PHP Chinese website!
How to get data in html
What is the difference between database views and tables
mobile phone screen tft
How to repair win7 system if it is damaged and cannot be booted
How to open pdb file
What is the space code in html
Why can swoole be resident in memory?
Solution to computer display error code 651