您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

微信小程序实现全局搜索代码高亮的示例

2024/3/11 0:05:54发布15次查看
本篇文章主要介绍了微信小程序实现全局搜索代码高亮的示例,现在分享给大家,也给大家做个参考。
需求
最近在做微信小程序的时候,需要实现在搜索框的输入内容的时候实现全局匹配实现高亮效果,目前的思路是,递归后台来返回的数据,并将对象的value值替换为需要的dom节点,并且通过rich-text来实现,高亮效果。
代码
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' }], // 可以是任何类型的数据 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, "<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) { // 每次输入来监听键盘,处理匹配的数据 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 }) } })
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
js+css实现打字效果
简单介绍react redux的中间件的使用
webpack源码之loader机制详解
以上就是微信小程序实现全局搜索代码高亮的示例的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product